Android界面事件响应Word文件下载.docx
《Android界面事件响应Word文件下载.docx》由会员分享,可在线阅读,更多相关《Android界面事件响应Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
![Android界面事件响应Word文件下载.docx](https://file1.bdocx.com/fileroot1/2022-11/16/34105eb0-f28d-449b-b75b-dacb467d688f/34105eb0-f28d-449b-b75b-dacb467d688f1.gif)
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事件响应
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;
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。
book"
s=s.replace("
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