0

在我的应用程序中,用户选择一个单选按钮,指示他有多少玩家想要参加这项选择。然后将该号码发送到另一个活动以进行显示。如果可能的话,我怎样才能获取单选按钮的 id 并去掉“单选”部分,只留下整数 4-10?

package teamBuilder;

import com.jonahwitcig.team.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;


public class CorrectedActivity extends Activity {
    /** Called when the activity is first created. */

    TextView player1, team1;
    Button next;
    RadioGroup RBGroup;
    RadioButton RB4, RB5, RB6, RB7, RB8, RB9, RB10;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.players);

        next = (Button) findViewById(R.id.next);
        //the name of a textView i will be displaying a number in
        team1 = (TextView) findViewById(R.id.team1);
        RB4 = (RadioButton) findViewById(R.id.radio4);
        RB5 = (RadioButton) findViewById(R.id.radio5);
        RB6 = (RadioButton) findViewById(R.id.radio6);
        RB7 = (RadioButton) findViewById(R.id.radio7);
        RB8 = (RadioButton) findViewById(R.id.radio8);
        RB9 = (RadioButton) findViewById(R.id.radio9);
        RB10 = (RadioButton) findViewById(R.id.radio10);
        //names of my radio buttons
        RBGroup = (RadioGroup) findViewById(R.id.RBGroup);



        RBGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup RBGroup, int checkedId) {

                team1.setText(checkedId);
            }
        });
4

1 回答 1

0

虽然不建议这样做(如果在大批量中进行反向查找会很慢,但如果很少进行则不会引起注意),这是正确的解决方案(请记住,您需要包含自己的错误检查/处理格式错误的标识符字符串):

String resName = getResources().getResourceEntryName(checkedId);
String number = resName.substring(5);
int num = Integer.parseInt(number);
于 2012-04-15T00:38:17.507 回答