初学安卓,次次卡在button的事件监听处。这次要一鼓作气,把这个处理点击事件的机制看得赤裸裸!
1 public void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_main); 4 btndo=(Button)findViewById(R.id.button1); 5 txtShow=(TextView)findViewById(R.id.textView1); 6 btndo.setOnClickListener(btndoListener); 7 8 } 9 10 private Button.OnClickListener btndoListener=new 11 Button.OnClickListener(){ 12 public void onClick (View v){ 13 txtShow.setText("你按到我了!"); 14 } 15 };
匿名内部类:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btndo=(Button)findViewById(R.id.button1); txtShow=(TextView)findViewById(R.id.textView1); btndo.setOnClickListener(new OnClickListener(){ public void onClick (View v){ txtShow.setText("你按到我了!"); } }); }
英里转换器
1 private Button btndo; 2 private TextView txtShow; 3 private EditText editdo; 4 @Override 5 public void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 btndo=(Button)findViewById(R.id.button1); 9 txtShow=(TextView)findViewById(R.id.textView2); 10 editdo=(EditText)findViewById(R.id.editView1); 11 btndo.setOnClickListener(new OnClickListener(){ 12 public void onClick (View v){ 13 int miles=Integer.parseInt(editdo.getText().toString()); 14 double km =1.61*(double)miles; 15 txtShow.setText("时速"+km+"公里"); 16 } 17 }); 18 19 20 }
多按钮共享事件 switch的应用
1 public void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_main); 4 bt1=(Button)findViewById(R.id.button1); 5 bt2=(Button)findViewById(R.id.button2); 6 bt3=(Button)findViewById(R.id.button3); 7 text1=(TextView)findViewById(R.id.textView1); 8 9 bt1.setOnClickListener(myListener); 10 bt2.setOnClickListener(myListener); 11 bt3.setOnClickListener(myListener); 12 13 } 14 private Button.OnClickListener myListener=new 15 Button.OnClickListener(){ 16 17 public void onClick(View v){ 18 String s=text1.getText().toString(); 19 switch(v.getId()){ 20 case R.id.button1: 21 text1.setText(s+"0"); 22 break; 23 24 case R.id.button2: 25 {text1.setText(s+"1"); 26 break; 27 } 28 case R.id.button3: 29 {text1.setText(s+"2"); 30 break; 31 } 32 } 33 } 34 35 };
tablelayout
1 public class MainActivity extends Activity { 2 private EditText edit1; 3 private Button b1,b2,b3,b4,b5,b6; 4 @Override 5 public void onCreate(Bundle savedInstanceState) { 6 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 edit1=(EditText)findViewById(R.id.editText1); 10 b1=(Button)findViewById(R.id.button1); 11 b2=(Button)findViewById(R.id.button2); 12 b3=(Button)findViewById(R.id.button3); 13 b4=(Button)findViewById(R.id.button4); 14 b5=(Button)findViewById(R.id.button5); 15 b6=(Button)findViewById(R.id.button6); 16 b1.setOnClickListener(listener); 17 b2.setOnClickListener(listener); 18 b3.setOnClickListener(listener); 19 b4.setOnClickListener(listener); 20 b5.setOnClickListener(listener); 21 b6.setOnClickListener(listener); 22 23 24 } 25 26 private Button.OnClickListener listener =new 27 Button.OnClickListener() { 28 29 @Override 30 public void onClick(View v) { 31 // TODO Auto-generated method stub 32 switch (v.getId()) { 33 case R.id.button1: 34 displayATM("1"); 35 break; 36 case R.id.button2: 37 displayATM("2"); 38 break; 39 case R.id.button3: 40 displayATM("3"); 41 break; 42 case R.id.button4: 43 displayATM("4"); 44 break; 45 case R.id.button5: 46 displayATM("5"); 47 break; 48 case R.id.button6: 49 displayATM("6"); 50 break; 51 52 default: 53 break; 54 } 55 } 56 }; 57 private void displayATM(String s){ 58 String str=edit1.getText().toString(); 59 edit1.setText(str+s); 60 }
**4.2**toast 弹出消息
1 public void onCreate(Bundle savedInstanceState) { 2 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 edit1=(EditText)findViewById(R.id.editText1); 6 b1=(Button)findViewById(R.id.button1); 7 b2=(Button)findViewById(R.id.button2); 8 b3=(Button)findViewById(R.id.button3); 9 b4=(Button)findViewById(R.id.button4); 10 b5=(Button)findViewById(R.id.button5); 11 b6=(Button)findViewById(R.id.button6); 12 b7=(Button)findViewById(R.id.button7); 13 b8=(Button)findViewById(R.id.button8); 14 15 b1.setOnClickListener(listener); 16 b2.setOnClickListener(listener); 17 b3.setOnClickListener(listener); 18 b4.setOnClickListener(listener); 19 b5.setOnClickListener(listener); 20 b6.setOnClickListener(listener); 21 b7.setOnClickListener(listener); 22 b8.setOnClickListener(listener); 23 24 25 26 } 27 28 private Button.OnClickListener listener =new 29 Button.OnClickListener() { 30 31 @Override 32 public void onClick(View v) { 33 // TODO Auto-generated method stub 34 switch (v.getId()) { 35 case R.id.button1: 36 displayATM("1"); 37 break; 38 case R.id.button2: 39 displayATM("2"); 40 break; 41 case R.id.button3: 42 displayATM("3"); 43 break; 44 case R.id.button4: 45 displayATM("4"); 46 break; 47 case R.id.button5: 48 displayATM("5"); 49 break; 50 case R.id.button6: 51 displayATM("6"); 52 break; 53 case R.id.button7:{ 54 String str=edit1.getText().toString(); 55 if (str.length()>0){ 56 str=str.substring(0,str.length()-1); 57 edit1.setText(str); 58 } 59 } 60 case R.id.button8:{ 61 String str=edit1.getText().toString(); 62 if (str.equals("667788")){ 63 Toast toast =Toast.makeText(MainActivity.this, "密码正确,欢迎使用提款功能", Toast.LENGTH_LONG); 64 toast.show(); 65 } 66 else{ 67 Toast toast =Toast.makeText(MainActivity.this, "密码错误,请重新输入", Toast.LENGTH_LONG); 68 toast.show(); 69 } 70 71 } 72 default: 73 break; 74 } 75 } 76 }; 77 private void displayATM(String s){ 78 String str=edit1.getText().toString(); 79 edit1.setText(str+s); 80 }
2)消息提示中央呈现
1 case R.id.button8:{ 2 String str=edit1.getText().toString(); 3 if (str.equals("123456")){ 4 Toast toast =Toast.makeText(MainActivity.this, "密码正确,欢迎使用提款功能", Toast.LENGTH_LONG); 5 toast.show(); 6 toast.setGravity(Gravity.CENTER, 0, 0); 7 } 8 else{ 9 Toast toast =Toast.makeText(MainActivity.this, "密码错误,请重新输入", Toast.LENGTH_LONG); 10 toast.show(); 11 toast.setGravity(Gravity.CENTER, 0, 0); 12 13 } 14 15 }
4.3 alertdialog对话框
1 case R.id.button9:{ 2 new AlertDialog.Builder(MainActivity.this) 3 .setTitle("确认窗口") 4 .setIcon(R.drawable.ic_launcher) 5 .setMessage("确定要结束程序?") 6 .show(); 7 }
2)加入交互按钮
1 case R.id.button9:{ 2 new AlertDialog.Builder(MainActivity.this) 3 .setTitle("确认窗口") 4 .setIcon(R.drawable.ic_launcher) 5 .setMessage("确定要结束程序?") 6 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 7 8 public void onClick(DialogInterface dialog, int which) { 9 10 finish(); 11 } 12 }) 13 .setNegativeButton("取消", new DialogInterface.OnClickListener() { 14 15 public void onClick(DialogInterface dialog, int which) { 16 17 18 } 19 }) 20 .show(); 21 break; 22 }
一道练习题
1 package com.twomeng.meng; 2 3 import android.R.raw; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.content.DialogInterface; 7 import android.location.GpsStatus.Listener; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.view.Gravity; 14 import android.view.Menu; 15 import android.view.MenuItem; 16 import android.widget.TextView; 17 import android.widget.Toast; 18 public class MainActivity extends Activity { 19 private Button b1,b2,b3,b4; 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 b1=(Button)findViewById(R.id.button1); 26 b2=(Button)findViewById(R.id.button2); 27 b3=(Button)findViewById(R.id.button3); 28 b4=(Button)findViewById(R.id.button4); 29 30 b1.setOnClickListener(Listener); 31 b2.setOnClickListener(Listener); 32 b3.setOnClickListener(Listener); 33 b4.setOnClickListener(Listener); 34 } 35 36 private Button.OnClickListener Listener= 37 new OnClickListener() { 38 39 @Override 40 public void onClick(View v) { 41 42 switch (v.getId()) { 43 case R.id.button1: 44 new AlertDialog.Builder(MainActivity.this) 45 .setTitle("确认窗口") 46 .setIcon(R.drawable.ic_launcher) 47 .setMessage("你按了button1,按确定关闭对话框") 48 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 49 50 @Override 51 public void onClick(DialogInterface dialog, int which) { 52 finish(); 53 54 } 55 }) 56 .show(); 57 break; 58 case R.id.button2:new AlertDialog.Builder(MainActivity.this) 59 .setTitle("确认窗口") 60 .setIcon(R.drawable.ic_launcher) 61 .setMessage("你按了button2,按确定关闭对话框") 62 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 63 64 @Override 65 public void onClick(DialogInterface dialog, int which) { 66 finish(); 67 68 } 69 }) 70 .show(); 71 break; 72 case R.id.button3: 73 new AlertDialog.Builder(MainActivity.this) 74 .setTitle("确认窗口") 75 .setIcon(R.drawable.ic_launcher) 76 .setMessage("你按了button3,按确定关闭对话框") 77 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 78 79 @Override 80 public void onClick(DialogInterface dialog, int which) { 81 finish(); 82 83 } 84 }) 85 .show(); 86 break; 87 88 case R.id.button4: 89 new AlertDialog.Builder(MainActivity.this) 90 .setTitle("确认窗口") 91 .setIcon(R.drawable.ic_launcher) 92 .setMessage("你按了button4,按确定关闭对话框") 93 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 94 95 @Override 96 public void onClick(DialogInterface dialog, int which) { 97 finish(); 98 99 } 100 }) 101 .show(); 102 break; 103 } 104 } 105 }; 106 107 @Override 108 public boolean onCreateOptionsMenu(Menu menu) { 109 // Inflate the menu; this adds items to the action bar if it is present. 110 getMenuInflater().inflate(R.menu.main, menu); 111 return true; 112 } 113 114 @Override 115 public boolean onOptionsItemSelected(MenuItem item) { 116 // Handle action bar item clicks here. The action bar will 117 // automatically handle clicks on the Home/Up button, so long 118 // as you specify a parent activity in AndroidManifest.xml. 119 int id = item.getItemId(); 120 if (id == R.id.action_settings) { 121 return true; 122 } 123 return super.onOptionsItemSelected(item); 124 } 125 }
第五章 单选、复选和下拉列表
1 private TextView text1; 2 private CheckBox check1,check2,check3; 3 4 public void onCreate(Bundle savedInstanceState) { 5 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 text1=(TextView)findViewById(R.id.textView1); 9 check1=(CheckBox)findViewById(R.id.check1); 10 check2=(CheckBox)findViewById(R.id.check2); 11 check3=(CheckBox)findViewById(R.id.check3); 12 13 check1.setOnCheckedChangeListener(listener); 14 check2.setOnCheckedChangeListener(listener); 15 check3.setOnCheckedChangeListener(listener); 16 17 } 18 private CheckBox.OnCheckedChangeListener listener = 19 new CheckBox.OnCheckedChangeListener() { 20 21 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 22 int n=0; 23 String s=""; 24 String s1="",s2="",s3=""; 25 if (check1.isChecked()){ 26 n++; 27 s1=check1.getText().toString()+" "; 28 } 29 else{ 30 s1=""; 31 } 32 if (check2.isChecked()){ 33 n++; 34 s2=check2.getText().toString()+" "; 35 } 36 else{ 37 s2=""; 38 } 39 if (check3.isChecked()){ 40 n++; 41 s3=check3.getText().toString()+" "; 42 } 43 else{ 44 s3=""; 45 } 46 s=s1+s2+s3; 47 text1.setText("最喜欢的球类有:"+s+"共"+n+"项"); 48 49 } 50 };
2)水平的复选框
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="我最喜欢的球类运动" 11 android:textSize="20sp" /> 12 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 13 xmlns:tools="http://schemas.android.com/tools" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:orientation="horizontal" > 17 <CheckBox 18 android:id="@+id/check1" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:text="篮球" 22 android:textSize="20sp" /> 23 24 <CheckBox 25 android:id="@+id/check2" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:text="足球" 29 android:textSize="20sp" /> 30 31 <CheckBox 32 android:id="@+id/check3" 33 android:layout_width="fill_parent" 34 android:layout_height="wrap_content" 35 android:text="棒球" 36 android:textSize="20sp" /> 37 </LinearLayout> 38 <TextView 39 android:id="@+id/textView1" 40 android:layout_width="fill_parent" 41 android:layout_height="wrap_content" 42 android:text="显示消息" 43 android:textSize="20sp" /> 44 45 </LinearLayout>
5.2 radiobutton radiogroup单选列表
1 private TextView text1; 2 private RadioGroup group; 3 private RadioButton button1,button2,button3; 4 public void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 text1=(TextView)findViewById(R.id.textView1); 8 group=(RadioGroup)findViewById(R.id.radioGroup1); 9 button1=(RadioButton)findViewById(R.id.radio0); 10 button2=(RadioButton)findViewById(R.id.radio1); 11 button3=(RadioButton)findViewById(R.id.radio2); 12 group.setOnCheckedChangeListener(listener); 13 14 } 15 private RadioGroup.OnCheckedChangeListener listener= 16 new RadioGroup.OnCheckedChangeListener() { 17 18 public void onCheckedChanged(RadioGroup group, int checkedId) { 19 int p=group.indexOfChild((RadioButton)findViewById(checkedId)); 20 int count=group.getChildCount(); 21 if(checkedId==R.id.radio0) 22 text1.setText(count+"项球类中,最喜欢第"+(p+1)+"项 "+button1.getText()); 23 if(checkedId==R.id.radio1) 24 text1.setText(count+"项球类中,最喜欢第"+(p+1)+"项 "+button2.getText()); 25 if(checkedId==R.id.radio2) 26 text1.setText(count+"项球类中,最喜欢第"+(p+1)+"项 "+button3.getText()); 27 } 28 };
2)水平单选框
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="我最喜欢的球类运动" 11 android:textSize="20sp" /> 12 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 13 xmlns:tools="http://schemas.android.com/tools" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:orientation="horizontal" > 17 <RadioGroup android:orientation="horizontal" 18 android:id="@+id/radioGroup1" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" > 21 22 <RadioButton 23 android:id="@+id/radio0" 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:text="篮球" /> 27 28 <RadioButton 29 android:id="@+id/radio1" 30 android:text="足球" 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 /> 34 35 <RadioButton 36 android:id="@+id/radio2" 37 android:text="棒球" 38 android:layout_width="fill_parent" 39 android:layout_height="wrap_content" 40 /> 41 </RadioGroup> 42 </LinearLayout> 43 44 <TextView 45 android:id="@+id/textView1" 46 android:layout_width="fill_parent" 47 android:layout_height="wrap_content" 48 android:text="显示消息" 49 android:textSize="20sp" /> 50 51 </LinearLayout>
5.3 spinner 下拉式列表
package com.twomeng.meng; import org.apache.http.conn.BasicEofSensorWatcher; import android.R.raw; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.location.GpsStatus.Listener; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView text1; private Spinner spinner; String[] Balls=new String[]{"篮球","足球","棒球","其他"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1=(TextView)findViewById(R.id.textView1); spinner=(Spinner)findViewById(R.id.spinner1); spinner=(Spinner)findViewById(R.id.spinner1); ArrayAdapter<String> adapterballs=new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,Balls); adapterballs.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapterballs); spinner.setOnItemSelectedListener(listener); } private Spinner.OnItemSelectedListener listener = new Spinner.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent,View v,int position,long id ){ String string=parent.getSelectedItem().toString(); text1.setText(string); } public void onNothingSelected(AdapterView<?> parent ){ } }; }
练习
1 package com.twomeng.meng; 2 3 import org.apache.http.conn.BasicEofSensorWatcher; 4 5 import android.R.raw; 6 import android.app.Activity; 7 import android.app.AlertDialog; 8 import android.content.DialogInterface; 9 import android.location.GpsStatus.Listener; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Adapter; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemSelectedListener; 16 import android.widget.ArrayAdapter; 17 import android.widget.Button; 18 import android.widget.CheckBox; 19 import android.widget.CompoundButton; 20 import android.widget.CompoundButton.OnCheckedChangeListener; 21 import android.widget.EditText; 22 import android.widget.RadioButton; 23 import android.widget.RadioGroup; 24 import android.widget.Spinner; 25 import android.view.Gravity; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 import android.widget.TextView; 29 import android.widget.Toast; 30 public class MainActivity extends Activity { 31 32 private TextView textView; 33 private CheckBox check1,check2,check3; 34 35 36 public void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.activity_main); 39 textView=(TextView)findViewById(R.id.textView2); 40 check1=(CheckBox)findViewById(R.id.checkBox1); 41 check2=(CheckBox)findViewById(R.id.checkBox2); 42 check3=(CheckBox)findViewById(R.id.checkBox3); 43 44 check1.setOnCheckedChangeListener(listener); 45 check2.setOnCheckedChangeListener(listener); 46 check3.setOnCheckedChangeListener(listener); 47 48 } 49 private CheckBox.OnCheckedChangeListener listener= 50 new OnCheckedChangeListener() { 51 52 @Override 53 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 54 int n=0; 55 String string; 56 String s1,s2,s3; 57 if (check1.isChecked()){ 58 n++; 59 s1=check1.getText().toString(); 60 61 } 62 else 63 s1=" "; 64 if (check2.isChecked()){ 65 n++; 66 s2=check2.getText().toString(); 67 68 } 69 else 70 s2=" "; 71 if (check3.isChecked()){ 72 n++; 73 s3=check3.getText().toString(); 74 75 } 76 else 77 s3=" "; 78 79 string=s1+s2+s3; 80 textView.setText("最喜欢的程序语言有:"+string+"共"+n+"种"); 81 } 82 }; 83 84 85 86 }
1 package com.twomeng.meng; 2 3 import org.apache.http.conn.BasicEofSensorWatcher; 4 5 import android.R.raw; 6 import android.app.Activity; 7 import android.app.AlertDialog; 8 import android.content.DialogInterface; 9 import android.location.GpsStatus.Listener; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Adapter; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemSelectedListener; 16 import android.widget.ArrayAdapter; 17 import android.widget.Button; 18 import android.widget.CheckBox; 19 import android.widget.CompoundButton; 20 import android.widget.CompoundButton.OnCheckedChangeListener; 21 import android.widget.EditText; 22 import android.widget.RadioButton; 23 import android.widget.RadioGroup; 24 import android.widget.Spinner; 25 import android.view.Gravity; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 import android.widget.TextView; 29 import android.widget.Toast; 30 public class MainActivity extends Activity { 31 32 private TextView textView; 33 private RadioGroup group; 34 private RadioButton b1,b2,b3,b4; 35 private EditText editText; 36 37 public void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 textView=(TextView)findViewById(R.id.textView2); 41 editText=(EditText)findViewById(R.id.editText1); 42 b1=(RadioButton)findViewById(R.id.radio0); 43 b2=(RadioButton)findViewById(R.id.radio1); 44 b3=(RadioButton)findViewById(R.id.radio2); 45 b4=(RadioButton)findViewById(R.id.radio3); 46 group=(RadioGroup)findViewById(R.id.radioGroup1); 47 group.setOnCheckedChangeListener(listener); 48 49 } 50 private RadioGroup.OnCheckedChangeListener listener= 51 new RadioGroup.OnCheckedChangeListener() { 52 53 public void onCheckedChanged(RadioGroup group, int checkedId) { 54 if (checkedId==R.id.radio0){ 55 String string=editText.getText().toString(); 56 textView.setText(string+" 你的血型为 "+b1.getText()); 57 } 58 if (checkedId==R.id.radio1){ 59 String string=editText.getText().toString(); 60 textView.setText(string+" 你的血型为 "+b2.getText()); 61 } 62 if (checkedId==R.id.radio2){ 63 String string=editText.getText().toString(); 64 textView.setText(string+" 你的血型为 "+b3.getText()); 65 } 66 if (checkedId==R.id.radio3){ 67 String string=editText.getText().toString(); 68 textView.setText(string+" 你的血型为 "+b4.getText()); 69 } 70 } 71 }; 72 73 }