猜猜我在想什麼(RadioButtonID)

猜猜我在想什麼(RadioButtonID)

新建一個繼承Activity類的RadioButtonIDActivity,並設置佈局文件爲:radiobuttonid.xml。

添加布局文件:

 

   <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/ageofxia"

        android:textSize="22sp" />

 

    <RadioGroup

        android:id="@+id/radiobuttonid_radiogroup"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" >

 

        <RadioButton

            android:id="@+id/radiobuttonid_rbtn01"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/twentyone" />

 

        <RadioButton

            android:id="@+id/radiobuttonid_rbtn02"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/twentytwo" />

 

        <RadioButton

            android:id="@+id/radiobuttonid_rbtn03"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/twentythree" />

    </RadioGroup>

 

    <Button

        android:id="@+id/radiobutton_btn01"

        style="@android:style/Widget.Button.Inset"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/sure"

        android:textSize="22sp" />

 

 

    <Button

        android:id="@+id/radiobutton_btn02"

        style="@android:style/Widget.Button.Inset"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/clear"

        android:textSize="22sp" />

 

    <TextView

        android:id="@+id/radiobutton_tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="22sp" />

 

在代碼中修改:

 

package lyx.feng.second;

 

import java.util.Random;

 

import lyx.feng.simpletextdemo.R;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

import android.widget.TextView;

 

public class RadioButtonIDActivity extends Activity implements

       OnCheckedChangeListener, OnClickListener {

    private TextView tv = null;

    private RadioGroup group = null;

    private Button btn01 = null;

    private Button btn02 = null;

    private int item[] = { R.id.radiobuttonid_rbtn01,

           R.id.radiobuttonid_rbtn02, R.id.radiobuttonid_rbtn03, };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.radiobuttonid);

       this.tv = (TextView) super.findViewById(R.id.radiobutton_tv);

       this.group = (RadioGroup) super

              .findViewById(R.id.radiobuttonid_radiogroup);

       this.btn01 = (Button) super.findViewById(R.id.radiobutton_btn01);

       this.btn02 = (Button) super.findViewById(R.id.radiobutton_btn02);

       this.group.setOnCheckedChangeListener(this);

       this.btn01.setOnClickListener(this);

       this.btn02.setOnClickListener(this);

    }

 

    @Override

    public void onCheckedChanged(RadioGroup group, int checkedId) {

 

    }

 

    @Override

    public void onClick(View v) {

       switch (v.getId()) {

       case R.id.radiobutton_btn01:

           int random = new Random().nextInt(3);

           RadioButton temp = (RadioButton) findViewById(this.group

                  .getCheckedRadioButtonId());

           //沒有選擇就不要繼續點了

           if (temp == null) {

              return;

           }

           RadioButton temp2 = (RadioButton) findViewById(item[random]);

           this.tv.setText("你選擇的是:" + temp.getText() + ",答案是:"

                  + temp2.getText());

           break;

       case R.id.radiobutton_btn02:

           this.group.clearCheck();

           this.tv.setText("");

           break;

       }

    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章