在RadioGroup中选择一个单选按钮 [英] Selecting a RadioButton in a Radiogroup

查看:229
本文介绍了在RadioGroup中选择一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid和我正在尝试使用radiogroups的示例程序。我已经宣布了RadioGroup中下2个单选按钮。我想这单选按钮的选择 RadioGroup中。如想使用它的,如果和匿名类的ELSEIF条件下新的onClickListener。

我想声明一个整型变量radio_selected,并得到radgrp.getCheckedRadioButtonId的返回值() radio_selected = radgrp.getCheckedRadioButtonId();并使用常数比较的if和else if条件为,

如果(radio_selected == R.id.rdb1和放大器;&安培;(edit1.getText()的toString()等于(管理员))及。&安培;(edit2.getText()的toString()。等于(管理员)))

我试图打印的radio_selected的价值。其打印我-1,这意味着空选择。但radgrp.getCheckedRadioButtonId();返回一个整数值。 但是上面的程序编译和工作正常,但我想知道为什么我不能用radio_selected不断做对比?

.java文件

 包android.button;


进口android.app.Activity;

进口android.os.Bundle;

进口android.text.Editable;

进口android.text.method.KeyListener;

进口android.util.Log;

进口android.view.KeyEvent;

进口android.view.View;

进口android.view.View.OnClickListener;

进口android.view.View.OnKeyListener;

进口android.widget.Button;

进口android.widget.EditText;

进口android.widget.RadioGroup;

进口android.widget.Toast;

进口android.widget.RadioGroup.OnCheckedChangeListener;


公共类Android_eg2延伸活动{


/ **第一次创建活动时调用。 * /

@覆盖

公共无效的onCreate(包savedInstanceState){

super.onCreate(savedInstanceState);

的setContentView(R.layout.main);

最终诠释radio_selected;


最后的EditText EDIT1 =(EditText上)findViewById(R.id.txt1);

最后的EditText EDIT2 =(EditText上)findViewById(R.id.txt2);

edit2.setOnKeyListener(新OnKeyListener(){


公共布尔onKey(视图V,INT关键code,KeyEvent的事件){

// TODO自动生成方法存根

如果(。edit2.getText()的toString()长度()→5){

Toast.makeText(getApplicationContext(),超过了最大限制,Toast.LENGTH_LONG).show();

}

返回false;

}

});

最后RadioGroup中radgrp =(RadioGroup中)findViewById(R.id.rad);

// radio_selected = radgrp.getCheckedRadioButtonId();

按钮BT =(按钮)findViewById(R.id.button);

bt.setOnClickListener(新OnClickListener(){


公共无效的onClick(视图v){

//System.out。的println(radgrp.getCheckedRadioButtonId());

//System.out.println(radio_selected);

// TODO自动生成方法存根

如果(R.id.button == v.getId()){

如果(radgrp.getCheckedRadioButtonId()== R.id.rdb1和放大器;及(edit1.getText()的toString()等于(管理员))及。&安培;(edit2.getText()的toString() .equals(管理员))){

Toast.makeText(getApplicationContext(),成功登录有关管理,Toast.LENGTH_LONG).show();

Log.v(TAG,在管理);

}

否则如果(radgrp.getCheckedRadioButtonId()== R.id.rdb2&安培;&安培;(edit1.getText()的toString()equalsIgnoreCase(用户))及&安培;(edit2.getText()的toString( ).equalsIgnoreCase(用户))){

Toast.makeText(getApplicationContext(),成功登录用户,Toast.LENGTH_LONG).show();

Log.v(TAG,用户);

}

其他{

Toast.makeText(getApplicationContext(),无效输入,Toast.LENGTH_LONG).show();

Log.v(TAG,无效);

}

}

}

});


radgrp.setOnCheckedChangeListener(新OnCheckedChangeListener(){


公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){

// TODO自动生成方法存根

开关(checkedId){

案例R.id.rdb1:

//Toast.makeText(getApplicationContext(),管理员,Toast.LENGTH_LONG).show();

案例R.id.rdb2:

//Toast.makeText(getApplicationContext(),用户,Toast.LENGTH_LONG).show();

}

}

});

}

}
 

的main.xml文件

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android

机器人:方向=垂直

机器人:layout_width =FILL_PARENT

机器人:layout_height =FILL_PARENT

>

<的TextView

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

机器人:文本=欢迎来到Wipro公司

/>


<的LinearLayout

机器人:方向=垂直

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

>


< RadioGroup中

机器人:ID =@ + ID /拉德

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

机器人:方向=横向

>

<单选按钮

机器人:ID =@ + ID / RDB1

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

机器人:文本=管理员

>

< /单选>

<单选按钮

机器人:ID =@ + ID / RDB2

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

机器人:文本=用户

>

< /单选>

< / RadioGroup中>


<的LinearLayout

机器人:方向=横向

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

>




<的TextView

机器人:ID =@ + ID / LBL1

机器人:layout_width =100dp

机器人:layout_height =WRAP_CONTENT

机器人:文本=用户名

机器人:TEXTSIZE =6PT

机器人:文字颜色=#00aa00

>


< / TextView的>


<的EditText

机器人:ID =@ + ID / TXT1

机器人:layout_width =100dp

机器人:layout_height =WRAP_CONTENT

机器人:文本=

机器人:TEXTSIZE =6PT

机器人:文字颜色=#AA0000

>

< /的EditText>


< / LinearLayout中>


<的LinearLayout

机器人:方向=横向

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

>

<的TextView

机器人:ID =@ + ID / LBL2

机器人:layout_width =100dp

机器人:layout_height =WRAP_CONTENT

机器人:文本=密码

机器人:TEXTSIZE =6PT

机器人:文字颜色=#00aa00

机器人:layout_weight =1


>


< / TextView的>


<的EditText

机器人:ID =@ + ID / TXT2

机器人:layout_width =100dp

机器人:layout_height =WRAP_CONTENT

机器人:文本=

机器人:TEXTSIZE =6PT

机器人:文字颜色=#AA0000

机器人:密码=真

>

< /的EditText>


< / LinearLayout中>


< / LinearLayout中>


<的LinearLayout

机器人:方向=横向

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

>

<按钮

机器人:ID =@ + ID /按钮

机器人:layout_width =WRAP_CONTENT

机器人:layout_height =WRAP_CONTENT

机器人:文本=提交

>

< /按钮>


< / LinearLayout中>


< / LinearLayout中>
 

R.java文件

  / *自动生成的文件。不要修改。

*

*此类被自动地被产生的

*从中发现资源数据AAPT工具。它

*不应该被手工修改。

* /


包android.button;


公共final类环R {

公共静态final类ATTR {

}

公共静态final类绘制{

公共静态最终诠释图标= 0x7f020000;

}

公共静态最后的类ID {

公共静态最终诠释按钮= 0x7f050007;

公共静态最终诠释LBL1 = 0x7f050003;

公共静态最终诠释LBL2 = 0x7f050005;

公共静态最终诠释弧度= 0x7f050000;

公共静态最终诠释RDB1 = 0x7f050001;

公共静态最终诠释RDB2 = 0x7f050002;

公共静态最终诠释TXT1 = 0x7f050004;

公共静态最终诠释TXT2 = 0x7f050006;

}

公共静态最后的客舱布局{

公共静态最终诠释主要= 0x7f030000;

}

公共静态final类的字符串{

公共静态最终诠释APP_NAME = 0x7f040001;

公共静态最终诠释你好= 0x7f040000;

}

}
 

解决方案

 布尔RAD1,RAD2 = FALSE;

 radio1.setOnCheckedChangeListener(新OnCheckedChangeListener(){

        公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
            // TODO自动生成方法存根
            如果(radio1.isChecked())
            {
                RAD1 = TRUE;
            }

        }
    });
radio2.setOnCheckedChangeListener(新OnCheckedChangeListener(){

        公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
            // TODO自动生成方法存根
            ifradio2.isChecked())
            {
                RAD2 = TRUE;
            }

        }
    });
 

I'm new to android and I was trying out an example program using the radiogroups. I have declared 2 radio buttons under the radiogroup. I wanted which radio button is selected in the radiogroup. as wanted to use it in the if and the elseif condition of the Anonymous class new onClickListener.

I tried to declare an integer variable radio_selected and to get the return value of the radgrp.getCheckedRadioButtonId() radio_selected = radgrp.getCheckedRadioButtonId(); and use the constant for comparision in the if and else if condition as,

if( radio_selected == R.id.rdb1 && (edit1.getText().toString().equals("admin")) && (edit2.getText().toString().equals("admin")))

I tried to print the value of radio_selected . Its printing me -1 which means empty selection. But radgrp.getCheckedRadioButtonId(); is returning a integer value. However the above program compiles and works fine but i wanted to know why i could not use radio_selected constant to do the comparision?

.java file

package android.button;


import android.app.Activity;

import android.os.Bundle;

import android.text.Editable;

import android.text.method.KeyListener;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnKeyListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioGroup;

import android.widget.Toast;

import android.widget.RadioGroup.OnCheckedChangeListener;


public class Android_eg2 extends Activity {


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

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final int radio_selected;


final EditText edit1 = (EditText)findViewById(R.id.txt1);

final EditText edit2 = (EditText)findViewById(R.id.txt2);

edit2.setOnKeyListener(new OnKeyListener() {


public boolean onKey(View v, int keyCode, KeyEvent event) {

// TODO Auto-generated method stub

if(edit2.getText().toString().length()>5){

Toast.makeText(getApplicationContext(), "EXCEEDED the MAX LIMIT", Toast.LENGTH_LONG).show();

}

return false;

}

});

final RadioGroup radgrp = (RadioGroup)findViewById(R.id.rad);

//radio_selected = radgrp.getCheckedRadioButtonId();

Button bt = (Button)findViewById(R.id.button);

bt.setOnClickListener(new OnClickListener() {


public void onClick(View v) {

//System.out. println(radgrp.getCheckedRadioButtonId());

//System.out.println(radio_selected);

// TODO Auto-generated method stub

if(R.id.button == v.getId()){

if( radgrp.getCheckedRadioButtonId() == R.id.rdb1 && (edit1.getText().toString().equals("admin")) && (edit2.getText().toString().equals("admin"))){

Toast.makeText(getApplicationContext(),"LOGIN SUCCESS FOR ADMIN",Toast.LENGTH_LONG).show();

Log.v("TAG","In admin");

}

else if(radgrp.getCheckedRadioButtonId() == R.id.rdb2 && (edit1.getText().toString().equalsIgnoreCase("user")) && (edit2.getText().toString().equalsIgnoreCase("user"))){

Toast.makeText(getApplicationContext(),"LOGIN SUCCESS FOR USER",Toast.LENGTH_LONG).show();

Log.v("TAG","In user");

}

else{

Toast.makeText(getApplicationContext(),"INVALID ENTRY",Toast.LENGTH_LONG).show();

Log.v("TAG","Invalid");

}

}

}

});


radgrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {


public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

switch(checkedId){

case R.id.rdb1:

//Toast.makeText(getApplicationContext(),"ADMIN",Toast.LENGTH_LONG).show();

case R.id.rdb2:

//Toast.makeText(getApplicationContext(),"USER",Toast.LENGTH_LONG).show();

}

}

});

}

}

main.xml file

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

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView 

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:text="Welcome to wipro"

/>


<LinearLayout

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>


<RadioGroup

android:id="@+id/rad"

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:orientation="horizontal"

>

<RadioButton

android:id="@+id/rdb1"

android:layout_width="wrap_content" 

android:layout_height="wrap_content"

android:text = "Admin"

>

</RadioButton>

<RadioButton

android:id="@+id/rdb2"

android:layout_width="wrap_content" 

android:layout_height="wrap_content"

android:text = "User"

>

</RadioButton>

</RadioGroup>


<LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>




<TextView 

android:id="@+id/lbl1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="USER NAME"

android:textSize="6pt"

android:textColor = "#00aa00"

>


</TextView>


<EditText

android:id="@+id/txt1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text=""

android:textSize="6pt"

android:textColor = "#aa0000"

>

</EditText>


</LinearLayout>


<LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

<TextView

android:id="@+id/lbl2"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="PASSWORD"

android:textSize="6pt"

android:textColor="#00aa00"

android:layout_weight="1"


>


</TextView>


<EditText

android:id="@+id/txt2"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text=""

android:textSize="6pt"

android:textColor = "#aa0000"

android:password = "true"

>

</EditText>


</LinearLayout>


</LinearLayout>


<LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SUBMIT"

>

</Button>


</LinearLayout>


</LinearLayout>

R.java file

/* AUTO-GENERATED FILE. DO NOT MODIFY.

*

* This class was automatically generated by the

* aapt tool from the resource data it found. It

* should not be modified by hand.

*/


package android.button;


public final class R {

public static final class attr {

}

public static final class drawable {

public static final int icon=0x7f020000;

}

public static final class id {

public static final int button=0x7f050007;

public static final int lbl1=0x7f050003;

public static final int lbl2=0x7f050005;

public static final int rad=0x7f050000;

public static final int rdb1=0x7f050001;

public static final int rdb2=0x7f050002;

public static final int txt1=0x7f050004;

public static final int txt2=0x7f050006;

}

public static final class layout {

public static final int main=0x7f030000;

}

public static final class string {

public static final int app_name=0x7f040001;

public static final int hello=0x7f040000;

}

}

解决方案

  boolean rad1,rad2=false;

 radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(radio1.isChecked())
            {
                rad1=true;
            }

        }
    });
radio2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            ifradio2.isChecked())
            {
                rad2=true;
            }

        }
    });

这篇关于在RadioGroup中选择一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆