我是android的新手,我正在尝试使用radiogroups的示例程序。我在放射组下面宣布了2个单选按钮。我想要选择哪个单选按钮 RadioGroup中。希望在Anonymous类new onClickListener的if和elseif条件中使用它。
我试图声明一个整数变量radio_selected并获取radgrp.getCheckedRadioButtonId()的返回值 radio_selected = radgrp.getCheckedRadioButtonId();并在if和else if条件中使用常量进行比较,
if(radio_selected == R.id.rdb1&&(edit1.getText()。toString()。equals(“admin”))&&(edit2.getText()。toString()。等于( “管理员”)))
我试图打印radio_selected的值。它打印我-1意味着空选。但是radgrp.getCheckedRadioButtonId();返回一个整数值。 但是上面的程序编译并正常工作,但我想知道为什么我不能使用radio_selected常量来进行比较?
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();
}
}
});
}
}
<?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>
/* 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;
}
}
答案 0 :(得分:2)
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;
}
}
});
答案 1 :(得分:1)
除此之外,我建议您在活动类中实现OnCheckedChangeListener,这将允许您覆盖onCheckedChanged方法以捕获单选按钮的选择。在您的情况下,您将radio_selected声明为最终变量,这是一个错误!最终意味着一旦定义了该变量,它的值就不能改变,所以即使它有效,它也会第一次工作而不是再次工作。因此,通过这些建议,您的活动可能如下所示:
//all of the imports
public class TestActivity extends Activity implements OnCheckedChangeListener{
private int radio_selected;//now radio_selected is a class variable
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
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(this); //you set this activity as the listener for the events on the radio group...
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rdb1:
radio_selected = R.id.rdb1;
Toast.makeText(getApplicationContext(),"ADMIN",Toast.LENGTH_LONG).show();
case R.id.rdb2:
radio_selected = R.id.rdb2;
Toast.makeText(getApplicationContext(),"USER",Toast.LENGTH_LONG).show();
}
}
}
请注意,每当选择一个单选按钮时,我都会设置radio_selected的值,这样一旦按下提交按钮,它就会被更新。 我希望我已经足够清楚,代码仍然可以改进,但这是另一个答案;) 干杯!
答案 2 :(得分:1)
RadioGroup radioGroup ;
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio1:
Toast.makeText(getApplicationContext(),"RadioButton1selected",Toast.LENGTH_LONG.show();
case R.id.radio2:
Toast.makeText(getApplicationContext(),"RadioButton2selected",Toast.LENGTH_LONG).show();}