6th-单选按钮小程序:简单的测试题

  这个小程序主要是对按钮(单选)的一个应用,加上了toast的功能。

修改的文档有所增加:

java:

 1 package com.example.hello2;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.Menu;
 7 import android.view.MenuItem;
 8 
 9 import android.widget.TextView;
10 import android.widget.RadioButton;
11 import android.widget.RadioGroup;
12 import android.widget.Toast;
13 import android.widget.RadioGroup.OnCheckedChangeListener;
14 
15 public class MainActivity extends ActionBarActivity {
16     TextView mTextView;
17     RadioGroup radioGroup;
18     RadioButton answer1;
19     RadioButton answer2;
20     RadioButton answer3;
21     RadioButton answer4;
22     
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27         
28         mTextView=(TextView)findViewById(R.id.myTextView);
29         mTextView.setText("请问1+1等于多少?");
30         
31         radioGroup=(RadioGroup)this.findViewById(R.id.radioGroup);
32         
33         answer1=(RadioButton)this.findViewById(R.id.answer1);
34         answer2=(RadioButton)this.findViewById(R.id.answer2);
35         answer3=(RadioButton)this.findViewById(R.id.answer3);
36         answer4=(RadioButton)this.findViewById(R.id.answer4);
37         
38         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
39             @Override
40             public void onCheckedChanged(RadioGroup group, int checkID){
41                 if(answer2.getId()==checkID){
42                     DisplayToast("你知道的太多了,嘻嘻");
43                 }else{
44                     DisplayToast("你答错啦!");
45                 }
46             }
47         });        
48     }
49     public void DisplayToast(String str){
50         Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
51         toast.setGravity(Gravity.TOP, 0, 220);
52         
53         toast.show();
54     }
55 
56     @Override
57     public boolean onCreateOptionsMenu(Menu menu) {
58         // Inflate the menu; this adds items to the action bar if it is present.
59         getMenuInflater().inflate(R.menu.main, menu);
60         return true;
61     }
62 
63     @Override
64     public boolean onOptionsItemSelected(MenuItem item) {
65         // Handle action bar item clicks here. The action bar will
66         // automatically handle clicks on the Home/Up button, so long
67         // as you specify a parent activity in AndroidManifest.xml.
68         int id = item.getItemId();
69         if (id == R.id.action_settings) {
70             return true;
71         }
72         return super.onOptionsItemSelected(item);
73     }
74 }

9-13行为增加的头文件

main.xml

 1 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.hello2.MainActivity" >
10 
11     <TextView
12         android:id="@+id/myTextView"
13         android:layout_width="fill_parent"
14         android:layout_height="wrap_content"/>
15 
16     <RadioGroup
17         android:id="@+id/radioGroup"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:layout_alignLeft="@+id/myTextView"
21         android:layout_below="@+id/myTextView"
22         android:layout_marginTop="14dp" >
23 
24         <RadioButton
25             android:id="@+id/answer1"
26             android:layout_width="wrap_content"
27             android:layout_height="wrap_content"
28             android:text="@string/answer1" />
29 
30         <RadioButton
31             android:id="@+id/answer2"
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content"
34             android:text="@string/answer2" />
35 
36         <RadioButton
37             android:id="@+id/answer3"
38             android:layout_width="wrap_content"
39             android:layout_height="wrap_content"
40             android:text="@string/answer3" />
41 
42         <RadioButton
43             android:id="@+id/answer4"
44             android:layout_width="wrap_content"
45             android:layout_height="wrap_content"
46             android:text="@string/answer4" />
47     </RadioGroup>
48 
49 </RelativeLayout>

设置radiogroup和radiobutton

string.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <string name="app_name">自我测评</string>
 4     <string name="hello">Welcome!</string>
 5     <string name="action_settings">Settings</string>
 6     <string name="answer1">等于1</string>
 7     <string name="answer2">等于2</string>
 8     <string name="answer3">等于3</string>
 9     <string name="answer4">等于4</string>
10 </resources>

结果:

学习感悟:

设置完radiogroup的参数后,不要忘了设置其监听器,这里选用OnCheckedChangeListener,监听单选按钮状态改变事件,单击单选按钮时,CheckedChangeListener的onCheckedChanged方法被触发,该方法包含了两个参数:

RadioGroup group 是RadioGroup的对象

int checkedID:当前发生状态改变的单选按钮的id

posted @ 2016-02-18 19:34  biyoner  阅读(1591)  评论(0编辑  收藏  举报