在Activity类中的使用:TextViewtextView1=(TextVi" />
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(Rhttps://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_height="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的创建在这里执行:@Overridepublic boolean onCreateOptionsMenu (Menu menu) {menu.add(0, 1, 1, R.string.back);menu.add(0,2,2,R.string.exit);returnsuper.onCreateOptio nsMenu(menu);}为按钮添加方法,需要实现Activity的onOptionsItemSelect ed方法:@Overridepublic boolean onOptionsItemSelect ed(MenuItem item) { if(item.getItemId() == 2){finish();}returnsuper.onOptionsItemSelected(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 (newRadioGroup.OnChecke dChangeListener() {@Overridepublic void onCheckedChanged(Ra dioGroup group, int checkedId) {if(radioButton1. getId() ==checkedId){System.out.println("radioButton1");}elseif(radioButton2.get Id() == checkedId){System.out.println("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)findViewById(R.id.checkBox2) ;为多选按钮添加监听器,这里使用CompoundButton.OnCh eckedChangeListenercheckBox1.setOnChec kedChangeListener(n ew CompoundButton.OnCh eckedChangeListener () {@Overridepublic void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {if(isChecked){System.out.println("checkBox1 is checked");}
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_height="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的创建在这里执行:@Overridepublic boolean onCreateOptionsMenu (Menu menu) {menu.add(0, 1, 1, R.string.back);menu.add(0,2,2,R.string.exit);returnsuper.onCreateOptio nsMenu(menu);}为按钮添加方法,需要实现Activity的onOptionsItemSelect ed方法:@Overridepublic boolean onOptionsItemSelect ed(MenuItem item) { if(item.getItemId() == 2){finish();}returnsuper.onOptionsItemSelected(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 (newRadioGroup.OnChecke dChangeListener() {@Overridepublic void onCheckedChanged(Ra dioGroup group, int checkedId) {if(radioButton1. getId() ==checkedId){System.out.println("radioButton1");}elseif(radioButton2.get Id() == checkedId){System.out.println("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)findViewById(R.id.checkBox2) ;为多选按钮添加监听器,这里使用CompoundButton.OnCh eckedChangeListenercheckBox1.setOnChec kedChangeListener(n ew CompoundButton.OnCh eckedChangeListener () {@Overridepublic void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {if(isChecked){System.out.println("checkBox1 is checked");}
android:id="@+id /editText1"
EditText editText1 =
(EditText)findViewB yId(R.id.editText1) ;
Button的使用方法
android:id="@+id /button1"android:layout_widt h="fill_parent"android:layout_height="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的创建在这里执行:@Overridepublic boolean onCreateOptionsMenu (Menu menu) {menu.add(0, 1, 1, R.string.back);menu.add(0,2,2,R.string.exit);returnsuper.onCreateOptio nsMenu(menu);}为按钮添加方法,需要实现Activity的onOptionsItemSelect ed方法:@Overridepublic boolean onOptionsItemSelect ed(MenuItem item) { if(item.getItemId() == 2){finish();}returnsuper.onOptionsItemSelected(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 (newRadioGroup.OnChecke dChangeListener() {@Overridepublic void onCheckedChanged(Ra dioGroup group, int checkedId) {if(radioButton1. getId() ==checkedId){System.out.println("radioButton1");}elseif(radioButton2.get Id() == checkedId){System.out.println("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)findViewById(R.id.checkBox2) ;为多选按钮添加监听器,这里使用CompoundButton.OnCh eckedChangeListenercheckBox1.setOnChec kedChangeListener(n ew CompoundButton.OnCh eckedChangeListener () {@Overridepublic void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {if(isChecked){System.out.println("checkBox1 is checked");}
android:id="@+id /button1"
android:layout_heig
ht="wrap_content"
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方法:
public boolean onOptionsItemSelect ed(MenuItem item) { if(item.getItemI
d() == 2){
finish();
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"/>
android:id="@+id /radioGroup1"
android:layout_widt h="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"/>
android:id="@+id /radioButton1"
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"/>
android:id="@+id /radioButton2"
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() {
public void onCheckedChanged(Ra dioGroup group, int checkedId) {
if(radioButton1. getId() ==
checkedId){
System.out.print
ln("radioButton1");
else
if(radioButton2.get Id() == checkedId)
{
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)findViewById(R.id.checkBox2) ;为多选按钮添加监听器,这里使用CompoundButton.OnCh eckedChangeListenercheckBox1.setOnChec kedChangeListener(n ew CompoundButton.OnCh eckedChangeListener () {@Overridepublic void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {if(isChecked){System.out.println("checkBox1 is checked");}
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)findViewById(R.id.checkBox2) ;为多选按钮添加监听器,这里使用CompoundButton.OnCh eckedChangeListenercheckBox1.setOnChec kedChangeListener(n ew CompoundButton.OnCh eckedChangeListener () {@Overridepublic void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {if(isChecked){System.out.println("checkBox1 is checked");}
/checkBox2"
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 () {
public void onCheckedChanged(Co mpoundButton buttonView, boolean isChecked) {
if(isChecked)
ln("checkBox1 is checked");