单选按钮(Android)

时间:2012-12-03 01:43:25

标签: android

所以,我在这里要做的是有一个主要的活动,如果满足要求(正确的用户名,密码和男性单选按钮被检查而不是女性),VM切换到“成功”活动。如果它不满足这3个要求中的任何一个,则当按下按钮时,VM将切换到“失败”活动。我有它正常工作,除了收音机按钮。

我在布局上创建了一个RadioGroup,但我不确定如何在类本身中实现它。我假设你必须找到ID,覆盖监听器等等......但是它无法正常工作。有任何想法吗?我在发布之前取出了大部分的RadioGroup属性,因此它不会那么混乱。

主要活动

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener
{
    Button button;
    EditText login;
    EditText password;
    RadioGroup mRadioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button1);
        login =(EditText)findViewById(R.id.editText1);
        password =(EditText)findViewById(R.id.editText2);
        button.setOnClickListener(new View.OnClickListener()  
    {
        @Override
        public void onClick(View view)
        {
            String L,P;             

            L = login.getText().toString();
            P = password.getText().toString();


            if(L.equals("name") && P.equals("123456"))
            {
                Intent intent = new Intent();  
                intent.setClass(MainActivity.this,Welcome.class);
                startActivity(intent);
            }
            else
            {
                Intent intent1 = new Intent();
                intent1.setClass(MainActivity.this,Failed.class);
                startActivity(intent1);
            }
      }   
    });




}



  /*  public void onRadioButtonClicked(View view) 
    {
        boolean checked = ((RadioButton) view).isChecked();

        switch(view.getId()) 
        {
            case R.id.radio1:
                if (checked)
                {
                Intent intent4 = new Intent();
                intent4.setClass(MainActivity.this,Welcome.class);
                startActivity(intent4);
                }
                break;
            case R.id.radio0:
                if (checked)
                {
                Intent intent2 = new Intent();
                intent2.setClass(MainActivity.this,Failed.class);
                startActivity(intent2);
                }
                break;
                }
                }
      */



    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    {
        // TODO Auto-generated method stub

    }


}

活动失败

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Failed extends Activity
{
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.failed);
        button = (Button)findViewById(R.id.button1);       
        button.setOnClickListener(new View.OnClickListener()


    {
        @Override
        public void onClick(View view)
        {
            Intent intent3 = new Intent();
            intent3.setClass(Failed.this,MainActivity.class);
            startActivity(intent3);
        }

    });
}
}

成功活动

import android.app.Activity;
import android.os.Bundle;

public class Welcome extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.success);
    }

}

3 个答案:

答案 0 :(得分:0)

尝试此操作以获取所选RadioButton的值,并使用它来检查代码中的特定条件:

private RadioGroup radioOptionGrp;
private RadioButton radioOptBtn;

//Get Reference to Radio group which holds the radio buttons
radioOptionGrp = (RadioGroup) findViewById(R.id.radioOpt);

//Get id of selected radioButton
int selectOptId = radioOptionGrp.getCheckedRadioButtonId();

//Get Reference to the Selected RadioButton
radioOptBtn = (RadioButton) findViewById(selectOptId);

//Get value of the selected Radio button
String value = radioOptBtn.getText().toString();    

希望这有帮助!!下面是一个示例代码示例,您可以从GitHub下载以查看RadioButtons如何工作https://github.com/asabbarwal/SimpleRadioButton

答案 1 :(得分:0)

我第一次见到RadioButtons太不容易了。但基本上,使用起来很简单。

private RadioGroup mRadioGroup;
private View radioButton;   
int radioButtonID;
int idx;            //index of radio item in the list

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
        mRadioGroup= (RadioGroup) findViewById(R.id.mRadioGroup);           
....
}

将这部分代码放到任何click事件方法中,idx总是会返回一个选中的单选按钮的索引:

radioButtonID = mRadioGroup.getCheckedRadioButtonId();
radioButton = mRadioGroup.findViewById(radioButtonID);
idx = mRadioGroup.indexOfChild(radioButton);

如果你想捕捉单选按钮点击事件,这里是一个很好的解决方案How to set On click listener on the Radio Button in android

答案 2 :(得分:0)

at first you can add two radio button in your activity like other form widgets
than try this code.
pay attention that when you click and set a radio button,you must disable other radio buttons.

arg1表示该按钮已被选中。

   package com.example.azarbaycannetworkcompany;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RadioButton first = (RadioButton) findViewById(R.id.radioButton1);
        final RadioButton seccond = (RadioButton) findViewById(R.id.radioButton2); 
        first.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1)
                {
                    seccond.setChecked(false);
                    Toast.makeText(getBaseContext(),"1 set shod",Toast.LENGTH_LONG).show();
                }

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

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1)
                {
                    first.setChecked(false);
                Toast.makeText(getBaseContext(),"2 set shod",Toast.LENGTH_LONG).show();

                }

            }
        });
    }


}