2016-10-10 2 views
0

Button을 클릭하면 TextView에있는 Radiobutton의 텍스트를 표시하는 방법은 무엇입니까? Android에서 Button을 누른 후 textView에서 Radiobutton의 텍스트를 표시하는 방법은 무엇입니까?

당신은 약간의 잘못을하고있는 내 코드,

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void onRadioButtonClicked(View view) { 

     boolean checked = ((RadioButton) view).isChecked(); 
     TextView tv1 = (TextView) findViewById(R.id.tv1); 

     switch(view.getId()) { 
      case R.id.rb1: 
       if (checked) 
        tv1.setText('rb1 text'); 
       break; 
      case R.id.rb2: 
       if (checked) 
        tv1.setText('rb2 text'); 
       break; 
     } 
    } 
} 

답변

0

입니다. 먼저 RadioButtonRadioGroup에 넣고 radioGroup에 대한 참조를 가져와야합니다. 마지막으로 해당 인스턴스에 OnCheckedChangeListener을 첨부하십시오. 이런 식으로 뭔가 :에서 onCreate에서

yourRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      switch(view.getId()) { 
       case R.id.rb1: 
       if (checked) 
        tv1.setText('rb1 text'); 
       break; 

       case R.id.rb2: 
       if (checked) 
        tv1.setText('rb2 text'); 
       break; 
    } 
     } 
    }); 
0

사용이() 메소드

최종 텍스트 뷰 yourTextView = (텍스트 뷰) findViewById를 (R.id.tv1);

RadioButton radioButton = (RadioButton) findViewById(R.id.radioBt); 
    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

      yourTextView.setText(isChecked ? "Checked" : "Unchecked"); 

     } 
    }); 
관련 문제