Android界面事件响应.docx
- 文档编号:3006547
- 上传时间:2022-11-17
- 格式:DOCX
- 页数:13
- 大小:19.11KB
Android界面事件响应.docx
《Android界面事件响应.docx》由会员分享,可在线阅读,更多相关《Android界面事件响应.docx(13页珍藏版)》请在冰豆网上搜索。
Android界面事件响应
1.Button事件响应
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState); //继承父类的onCreate()方法
setContentView(R.layout.activity_main); //指定使用用户的UI界面文件为activity_main.xml
Buttonbtn=(Button)findViewById(R.id.button1); //通过findViewById方法获得Button1的对象
btn.setOnClickListener(newOnClickListener(){ //使用OnClickListener接口作为监听器,新建并实现匿名类
publicvoidonClick(Viewv){
setTitle("设置标题");
Log.i("Main_Activity","设置LogCat中打印的字符"); //黄色部分为活动名,红色部分设置输出内容
TextViewtext=(TextView)findViewById(R.id.textview1);
text.setText("设置输出文字"); //输出文字到text
text.setTextColor(Color.RED); //设置显示字体的颜色
text.setTextSize(TypedValue.COMPLEX_MANTISSA_MASK,20); //设置显示字体大小
text.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); //设置显示字体的风格
Intentintent=newIntent
(MainActivity.this,NewActivity.class);
startActivity(intent); //启动新的Activity
} //onClick方法结束
}); //实现匿名类
} //onCreate方法结束
/*使用Button打开新Activity引用
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
*/
2.EditText事件响应
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);
TextViewtv=(TextView)findViewById(R.id.textview1);
EditTextet=(EditText)findViewById(R.id..editText1);
et.addTextChangedListener(newTextWatcher(){ //watcher['wɒtʃə(r)]n观察者
publicvoidafterTextChanged(Editables){ //editable['ɛdɪtəbl]adj可编辑的
/*thismethodiscalledtonotifyyouthat,somewherewithins,thetexthasbeenchanged
调用此方法来通知你,文本在某个地方已经更改*/
}
publicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){ //sequence['siːkw(ə)ns]n顺序
/*thismethodiscalledtonotifyyouthat,withins,thecountcharactersbeginningatstartareabouttobereplacebynewtextwithlengthafte
调用此方法来通知你,在字符顺序、字符计数开始即将取代后通过新的文本长度 */
}
publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){
/*thismethodiscalledtonotifyyouthat,withins,thecountcharactersbeginningatstarthavejustreplacedoldtextthathadlengthbefore
调用此方法来通知你,在字符顺序、字符计数开始之前就取代了旧的文本长度*/
Stringtext=et.getText().toString();
tv.setText(text);
}
});
3.CheckBox事件响应
publicclassCheckBoxActivityextendsActivity{
privateTextViewtv;
privateCheckBoxbook,song,football;
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
tv=(TextView)findViewById(R.id.textview1);
book=(CheckBox)findViewById(checkbox1);
song=(CheckBox)findViewById(checkbox2);
football=(CheckBox)findViewById(checkbox3);
book.setOnCheckedChangeListener(newCompundButton.OnCheckedChangeListener(){
/*匿名类CompundButton使用了OnCheckedChangeListener()接口
CompundButton.OnCheckedChangeListener
一个带有选中/未选中状态的按钮。
当按钮按下或点中时自动改变状态 */
publicvoidonCheckedChanged(CompundButtonbuttonView,bolleanisChecked){ /*publicabstractvoidonCheckedChanged在按钮选中状态发生改变时被调用*/
if(book.isChecked()){
tv.append
(book.getText().toString());}
else{
if(tv.getText().toString().contains("看书"));
/*publicbooleancontains(CharSequences)
判断指定字符串是否包含指定字符序列,如果包含返回true,否则返回false.
forexample:
Strings="thisismyfirstjavaapplication";
booleana=str.contains("java");*/
{tv.setText(tv.getText().toString().replace("看书",""));
/*publicStringreplace(CharoldChar,charnewChar)
方法将字符串str中的字符串A替换成字符串B。
forexample:
Strings="book";s=s.replace("book","reading");*/
} //if语句结束
} //else语句结束
} //onCheckedChanged方法结束
}); //匿名类CompundButton.OnCheckedChangeListener结束
song.setOnCheckedChangeListener(newCompundButton.OnCheckedChangeListener(){
publicvoidonCheckedChanged(CompundButtonbuttonView,booleanisChecked){
......}
}); //对所有组件一一监听
4.单项选择按钮组(RadioGroup)
publicclassMainActivityextendsActivity{
RadioGroupgroup; /*group是类MainActivity的成员,在MainActivity中可以被调用。
匿名类RadioGroup的方法只能使用RadioGroup的成员,
故TextView、RadioButton的对象应该在RadioGroup中定义*/
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group=(RadioGroup)findViewById(R.id.radioGroup1);
group.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener(){
/*匿名类RadioGroup使用了OnCheckedChangeListener接口*/
TextViewhint=(TextView)findViewById(R.id.textView1);
RadioButtonfirst=(RadioButton)findViewById(R.id.radio0);
RadioButtonsecond=(RadioButton)findViewById(R.id.radio1);
RadioButtonthird=(RadioButton)findViewById(R.id.radio2);
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
StringgroupText="实例:
";
if(checkedId==first.getId()){
/*使用if...elseif方法查看用户点击的是哪一个按钮。
这里用到两个方法:
getId()返回按钮的Id值;getText()返回按钮上的字符串;*/
groupText+=first.getText().toString();
hint.setText
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 界面 事件 响应