4.6向左或向右RadioGroup组与onCheckedChanged事件

 

目录

4.6向左或向右---RadioGroup组与onCheckedChanged事件... 1

目标:1

方法:1

代码:1

效果:5

 

目标:

         可以在选择RadioButton的同时进行事件处理.

方法:

RadioGroupsetOnCheckedChangeListener方法

ButtonsetOnClickListener方法.

代码:

RadioGroupActivity.java

package edu.cquptzx.RadioGroup;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;

import android.widget.Toast;

 

publicclass RadioGroupActivity extends Activity {

    private TextView tv;

    private RadioGroup rg;

    private RadioButton rb1,rb2;

    private Button clearBtn,answerBtn;

    /** Called when the activity is first created. */

    @Override

   

        publicvoid onCreate(Bundle savedInstanceState)

        {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

           

            //根据ID找到对象

          tv = (TextView) findViewById(R.id.textView);

          rg = (RadioGroup) findViewById(R.id.myRadioGroup);

          rb1 = (RadioButton) findViewById(R.id.radioButton1);

          rb2 = (RadioButton) findViewById(R.id.radioButton2);

          clearBtn = (Button) findViewById(R.id.ClearButton);

          answerBtn = (Button) findViewById(R.id.AnswerButton);

         

          rg.setOnCheckedChangeListener(rgOnCheckedChangeListener);

          clearBtn.setOnClickListener(clearBtnOnClickListener);

          answerBtn.setOnClickListener(answerBtnOnClickListener);

        }

   

     //选中RadioButton的同时进行事件处理:这里的例子是改变TextView的文字内容.

        private RadioGroup.OnCheckedChangeListener rgOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener()

        {

          

           @Override

           publicvoid onCheckedChanged(RadioGroup group, int checkedId)

           {

              if(checkedId == rb1.getId())

              {

                  tv.setText(rb1.getText());

              }

              elseif (checkedId == rb2.getId())

              {

                  tv.setText(rb2.getText());

              }

           }

       };

      

      

       //清除选中状态.

       private Button.OnClickListener clearBtnOnClickListener = new Button.OnClickListener()

       {

           publicvoid onClick(View v)

           {  

              rg.clearCheck();

              tv.setText(getString(R.string.who));

           }         

       };

      

       //回答问题.

       private Button.OnClickListener answerBtnOnClickListener = new Button.OnClickListener()

       {

           publicvoid onClick(View v)

           {  

              //回答正确

              if(rb1.isChecked())

              {

                  //tv.setText(rb1.getText());

                  new AlertDialog.Builder(RadioGroupActivity.this)

                  .setIcon(R.drawable.right)

                  .setTitle(R.string.Dialog_Title_Right)

                  .setMessage(R.string.Dialog_Message_Right)

                  .setPositiveButton("ok", null)

                  .show();

                  tv.setText(getString(R.string.who));

              }

             

              //回答错误.

              if (rb2.isChecked())

              {

                  //tv.setText(rb2.getText());

                  new AlertDialog.Builder(RadioGroupActivity.this)

                  .setIcon(R.drawable.wrong)

                  .setTitle(R.string.Dialog_Title_Wrong)

                  .setMessage(R.string.Dialog_Message_Wrong)

                  .setPositiveButton("ok", null)

                  .show();

                  tv.setText(getString(R.string.who));

              }

             

              //什么都没选

              if(rb1.isChecked()== false && rb2.isChecked()==false)

              {

                  System.out.println("A");

 

                  Toast.makeText(getBaseContext(),"好像什么都没有选哦",Toast.LENGTH_LONG).show();

                  System.out.println("B");

              }

           }         

       };

}

Main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/who"

        android:textSize="22dp"

        android:textColor="#FF00FF"/>

    <RadioGroup

    android:id="@+id/myRadioGroup"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical">

     <RadioButton

         android:id="@+id/radioButton1"

         android:layout_width="match_parent"

         android:layout_height="wrap_content"

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

 

    <RadioButton

        android:id="@+id/radioButton2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

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

    </RadioGroup>

 

    <LinearLayout

        android:id="@+id/linearLayout1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

 

    <Button

        android:id="@+id/ClearButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="left"

        android:gravity="left"

        android:text="清除" />

 

 

 

    <Button

        android:id="@+id/AnswerButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:gravity="right"

        android:text="回答" />

 

    </LinearLayout>

 

</LinearLayout>

效果:

RadioGroup

For more details , contacts me by :

cquptzx@qq.com or  cquptzx@outlook.com

 

原文地址:https://www.cnblogs.com/xilifeng/p/2657414.html