检查RadioButton是否被选中后,按钮不会切换活动

时间:2013-08-16 16:34:28

标签: java android button

这是我的第一个程序,它是一个硬币翻转程序。我正在尝试编写主按钮以检查选择了哪个RadioButton,然后根据该信息转到正确的活动。我已经坚持了很长一段时间,并希望得到任何反馈。谢谢。

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.coin_select, menu);
    return true;}
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coin_select);
    Button goFlipCoin;
    goFlipCoin = (Button) findViewById(R.id.goFlip);
    goFlipCoin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            View selectPenney = (RadioButton) findViewById(R.id.selectP);
            View selectNickel = (RadioButton) findViewById(R.id.selectN);
            View selectDime = (RadioButton) findViewById(R.id.selectD);
            View selectQuarter = (RadioButton) findViewById(R.id.selectQ);
            View selectOne = (RadioButton) findViewById(R.id.selectO);

            if(selectPenney.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, PennyFlip.class));
            }
            else if(selectNickel.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, NickelFlip.class));
            }
            else if(selectDime.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, DimeFlip.class));
            }
            else if(selectQuarter.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, QuarterFlip.class));
            }
            else if(selectOne.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, DollarFlip.class));
            }
        }
    });
    }
}

另外,我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flipacoin"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.flipacoin.CoinSelect"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.flipacoin.PennyFlip"
        android:label="@string/title_activity_penny_flip" >
    </activity>
    <activity
        android:name="com.example.flipacoin.NickelFlip"
        android:label="@string/title_activity_nickel_flip" >
    </activity>
    <activity
        android:name="com.example.flipacoin.DimeFlip"
        android:label="@string/title_activity_dime_flip" >
    </activity>
    <activity
        android:name="com.example.flipacoin.QuarterFlip"
        android:label="@string/title_activity_quarter_flip" >
    </activity>
    <activity
        android:name="com.example.flipacoin.DollarFlip"
        android:label="@string/title_activity_dollar_flip" >
    </activity>
</application>

2 个答案:

答案 0 :(得分:1)

isSelected()更改为 isChecked()

答案 1 :(得分:0)

尝试以下条件中的更改,它应该适合您:

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

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coin_select);
    Button goFlipCoin;
    goFlipCoin = (Button) findViewById(R.id.goFlip);
    goFlipCoin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            View selectPenney = (RadioButton) findViewById(R.id.selectP);
            View selectNickel = (RadioButton) findViewById(R.id.selectN);
            View selectDime = (RadioButton) findViewById(R.id.selectD);
            View selectQuarter = (RadioButton) findViewById(R.id.selectQ);
            View selectOne = (RadioButton) findViewById(R.id.selectO);

            if(v==selectPenney && v.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, PennyFlip.class));
            }
            else if(v==selectNickel && v.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, NickelFlip.class));
            }
            else if(v==selectDime && v.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, DimeFlip.class));
            }
            else if(v==selectQuarter && v.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, QuarterFlip.class));
            }
            else if(v==selectOne && v.isSelected())
            {
                startActivity(new Intent(CoinSelect.this, DollarFlip.class));
            }
        }
    });
    }
}