0

这是我的第一个程序,它是一个抛硬币程序。我正在尝试对主按钮进行编码以检查选择了哪个 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>

4

2 回答 2

1

将isSelected()更改为isChecked()

于 2013-08-16T16:41:01.920 回答
0

尝试以下 if 条件的更改,它应该对您有用:

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));
            }
        }
    });
    }
}
于 2013-08-16T16:44:43.793 回答