1、Android界面事件响应1.Button事件响应protected void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState); /继承父类的onCreate()方法setContentView(R.layout.activity_main); /指定使用用户的UI界面文件为activity_main.xmlButton btn=(Button)findViewById(R.id.button1); /通过findViewById方法获得Button1的对象btn.setOnClickListener(
2、new OnClickListener() /使用OnClickListener接口作为监听器,新建并实现匿名类public void onClick(View v)setTitle(设置标题); Log.i(Main_Activity,设置LogCat中打印的字符); /黄色部分为活动名,红色部分设置输出内容TextView text=(TextView)findViewById(R.id.textview1);text.setText(设置输出文字); /输出文字到texttext.setTextColor(Color.RED); /设置显示字体的颜色text.setTextSize(Ty
3、pedValue.COMPLEX_MANTISSA_MASK,20); /设置显示字体大小text.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD); /设置显示字体的风格Intent intent=new Intent(MainActivity.this,NewActivity.class);startActivity(intent); /启动新的Activity /onClick方法结束); /实现匿名类 /onCreate方法结束/*使用Button打开新Activity引用import android.os.Bundle;impo
4、rt android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;*/2.EditText事件响应protected void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.n
5、ew_activity);TextView tv=(TextView)findViewById(R.id.textview1);EditText et=(EditText)findViewById(R.id.editText1);et.addTextChangedListener(new TextWatcher() /watcherwt(r)n观察者public void afterTextChanged(Editable s) /editabledtbladj可编辑的/*this method is called to notify you that ,somewhere within s,
6、the text has been changed调用此方法来通知你,文本在某个地方已经更改*/public void beforeTextChanged(CharSequence s,int start,int count,int after) /sequencesikw()nsn顺序/*this method is called to notify you that,within s,the count characters beginning at start are about to be replace by new text with length afte调用此方法来通知你,在字
7、符顺序、字符计数开始即将取代后通过新的文本长度*/public void onTextChanged(CharSequence s,int start,int before,int count)/*this method is called to notify you that,within s,the count characters beginning at start have just replaced old text that had length before调用此方法来通知你,在字符顺序、字符计数开始之前就取代了旧的文本长度*/String text=et.getText().
8、toString();tv.setText(text););3.CheckBox事件响应public class CheckBoxActivity extends Activityprivate TextView tv;private CheckBox book,song,football;protected void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.checkbox);tv=(TextView)findViewById(R.id.text
9、view1);book=(CheckBox)findViewById(checkbox1);song=(CheckBox)findViewById(checkbox2);football=(CheckBox)findViewById(checkbox3);book.setOnCheckedChangeListener(new CompundButton.OnCheckedChangeListener() /*匿名类CompundButton使用了OnCheckedChangeListener()接口CompundButton.OnCheckedChangeListener一个带有选中/未选中状
10、态的按钮。当按钮按下或点中时自动改变状态*/public void onCheckedChanged(CompundButton buttonView,bollean isChecked) /*public abstract void onCheckedChanged在按钮选中状态发生改变时被调用*/if(book.isChecked()tv.append(book.getText().toString();elseif(tv.getText().toString().contains(看书); /*public boolean contains(CharSequence s)判断指定字符串是
11、否包含指定字符序列,如果包含返回true,否则返回false .for example:String s=this is my first java application;boolean a=str.contains(java);*/ tv.setText(tv.getText().toString().replace(看书,);/*public String replace(Char oldChar,char newChar)方法将字符串str中的字符串A替换成字符串B。for example:String s=book;s=s.replace(book,reading);*/ /if语句
12、结束 /else语句结束 /onCheckedChanged方法结束); /匿名类CompundButton.OnCheckedChangeListener结束song.setOnCheckedChangeListener(new CompundButton.OnCheckedChangeListener()public void onCheckedChanged(CompundButton buttonView,boolean isChecked). .); /对所有组件一一监听4.单项选择按钮组(RadioGroup)public class MainActivity extends Ac
13、tivity RadioGroup group; /*group是类MainActivity的成员,在MainActivity中可以被调用。匿名类RadioGroup的方法只能使用RadioGroup的成员,故TextView、RadioButton的对象应该在RadioGroup中定义*/Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); group=(RadioGroup)f
14、indViewById(R.id.radioGroup1); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() /*匿名类RadioGroup使用了OnCheckedChangeListener接口*/TextView hint=(TextView)findViewById(R.id.textView1); RadioButton first=(RadioButton)findViewById(R.id.radio0); RadioButton second=(RadioButton)findVi
15、ewById(R.id.radio1); RadioButton third=(RadioButton)findViewById(R.id.radio2); public void onCheckedChanged(RadioGroup group,int checkedId) String groupText=实例:; if(checkedId= =first.getId()/*使用if.else if方法查看用户点击的是哪一个按钮。这里用到两个方法:getId()返回按钮的Id值;getText()返回按钮上的字符串;*/ groupText+=first.getText().toString(); hint.setText
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1