在我的应用程序中,用户选择一个单选按钮,指示他想要参加这项运动的玩家数量。然后将该号码发送到另一个要显示的活动。如果可能的话,我怎么能取出单选按钮的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);
}
});
答案 0 :(得分:0)
虽然不推荐这样做(如果在大批量中完成反向查找非常慢,但如果很少进行则反复查找会很明显),这是正确的解决方案(请记住,您需要包含自己的错误检查/处理格式错误的标识符字符串):
String resName = getResources().getResourceEntryName(checkedId);
String number = resName.substring(5);
int num = Integer.parseInt(number);