RadioButton,将Int转移到另一个Activity

时间:2015-03-29 21:06:07

标签: android

尝试制作单选按钮,传输整数并在另一个Activity中的TextView中设置它 点击按钮后。

这是一项活动:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button B = (Button)findViewById(R.id.button);
}
    public void Set(View v){
  RadioButton RBB = (RadioButton)findViewById(R.id.RB1);
  TextView T1=(TextView)findViewById(R.id.R1);
  if(RBB.isChecked()== true){
      Intent IL = new Intent(this,Transfer.class);
      int C = 10;
      IL.putExtra("C",C);
      startActivity(IL);
  }}
    }

这是活动B:

public class Transfer extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.transfer);
    TextView TB = (TextView)findViewById(R.id.textView5);
    Bundle extra =getIntent().getExtras();
    int Y = extra.getInt("C");
    TB.setText(Y);


}
 }

2 个答案:

答案 0 :(得分:0)

变化

TB.setText(Y);

TB.setText(Integer.toString(Y));

将整数传递给TextView时,它认为它是strings.xml中的资源ID

答案 1 :(得分:0)

TB.setText(Y+"");活动中使用Transfer