在Activity类中的使用:TextViewtextView1=(TextVi" />
文档库 最新最全的文档下载
当前位置:文档库 › Android中的常用控件及其基本用法

Android中的常用控件及其基本用法

Android中的常用控件及其基本用法

TextView的使用方法

布局文件中的配置:

android:id="@+id /textView1"

android:layout_widt h="fill_parent"

android:layout_heig ht="wrap_content"

android:text="@stri ng/arthinking"

/>

在Activity类中的使用:

TextView textView1 = (TextView)findViewB yId(R.id.textView1); textView1.setText(R

https://www.wendangku.net/doc/2d18338582.html,ername); EditText的使用方法

布局文件中的配置:

android:id="@+id /editText1"

android:layout_widt h="fill_parent"

android:layout_heig ht="wrap_content"

/>

在Activity类中的使用:

EditText editText1 =

(EditText)findViewB yId(R.id.editText1) ;

Button的使用方法

布局文件中的配置:

android:id="@+id /button1"

android:layout_widt h="fill_parent"

android:layout_heig

ht="wrap_content"

/>

在Activity类中的使用:

Button button1 = (Button)findViewByI d(R.id. button1);

//TestListener为继承OnClickListener的类

button1.setOnClickL istener(new TestListener());

Menu的使用方法

onCreateOptionsMenu (Menu menu)

Initialize the contents of the Activity's standard options menu.

onCreateOptionsMenu 是Activity中的一个方法,当用户点击了MENU按钮时,Activity会触发该方法,Menu的创建在这里执行:

@Override

public boolean onCreateOptionsMenu (Menu menu) {

menu.add(0, 1, 1, R.string.back);

menu.add(0,2,2,R

.string.exit);

return

super.onCreateOptio nsMenu(menu);

}

为按钮添加方法,需要实现

Activity的onOptionsItemSelect ed方法:

@Override

public boolean onOptionsItemSelect ed(MenuItem item) { if(item.getItemI

d() == 2){

finish();

}

return

super.onOptionsItem

Selected(item);

}

RadioGroup和RadioButton

布局文件的编写:

android:id="@+id /radioGroup1"

android:layout_widt h="wrap_content"

android:layout_heig ht="wrap_content"

android:orientation ="vertical"

>

android:id="@+id /radioButton1"

android:layout_widt h="wrap_content"

android:layout_heig ht="wrap_content"

android:text="@stri ng/female"

/>

android:id="@+id /radioButton2"

android:layout_widt h="wrap_content"

android:layout_heig ht="wrap_content"

android:text="@stri ng/male"

/>

在Activity中使用:

RadioGroup radioGroup1 = (RadioGroup)findVie wById(R.id.radioGro up1);

RadioButton radioButton1 =

(RadioButton)findVi ewById(R.id.femaleB utton);

RadioButton radioButton2 = (RadioButton)findVi ewById(R.id. radioButton2);

为RadioGroup设置监听器,使用

RadioGroup.OnChecke dChangeListener类

radioGroup1.setOnCh eckedChangeListener (new

RadioGroup.OnChecke dChangeListener() {

@Override

public void onCheckedChanged(Ra dioGroup group, int checkedId) {

if(radioButton1. getId() ==

checkedId){

System.out.print

ln("radioButton1");

}

else

if(radioButton2.get Id() == checkedId)

{

System.out.print

ln("radioButton2");

}

}

});

CheckBox

布局文件的编写:

android:id="@+id

/checkBox1"

android:layout_w idth="wrap_content"

android:layout_h

"

android:text="@s tring/checkBox1"

/>

android:id="@+id

/checkBox2"

android:layout_w idth="wrap_content"

android:layout_h

"

android:text="@s tring/checkBox2"

/>

在Activity中的使用:

CheckBox checkBox1 = (CheckBox)findViewB yId(R.id.checkBox1); CheckBox checkBox2 = (CheckBox)findViewB

yId(R.id.checkBox2) ;

为多选按钮添加监听器,这里使用CompoundButton.OnCh eckedChangeListener

checkBox1.setOnChec kedChangeListener(n ew CompoundButton.OnCh eckedChangeListener () {

@Override

public void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {

if(isChecked)

{

System.out.print

ln("checkBox1 is checked");

}

相关文档
相关文档 最新文档