2017-02-27 130 views
1

因此,我试图编写代码来显示单选按钮单击寄存器。理想情况下,当选择单选按钮时,TextView的内容将会改变。首先,我有一个'北'的单选按钮。当选中North时,TextView的内容将变为'North'。我知道有行动监听者参与,但我对Java不熟悉。这肯定会弹出我的Java樱桃。话虽如此,我写的代码不起作用。任何人都可以告诉我,如果我在正确的轨道上,或提供一些建议?请注意,这不适用于班级分配。这是一个非常开放的课程项目,我正在与另一个人一起工作。Android:使用单选按钮更改文本视图的内容

public class MainActivity extends AppCompatActivity { 

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

} 
public void onRadioButtonClicked(View v){ 
    TextView text = (TextView)findViewById(R.id.text); 

    RadioButton N = (RadioButton) findViewById(R.id.north); 

    //evaluates 'checked' value of radio button 
    boolean checked = ((RadioButton) v).isChecked(); 

if(N.isChecked()){ 

    text.setText("N"); 
} 
} 

}

+0

也显示您的XML。请记住将您的XML中的'onClick'侦听器设置为'onRadioButtonClicked' –

回答

1

尝试

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.your_radio_group_id); 
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     switch (checkedId) { 
      case R.id.north ; 
       // set text North for your textview here 
       break; 
      case R.id.another_radio_button_id: 
       // do something 
       break; 
     } 
    } 
}); 
2

要使用RadioButton正常,你最好组一帮RadioButton s转换为一组,命名为RadioGroup

<RadioGroup 
    android:id="@+id/rg1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <RadioButton 
     android:id="@+id/rg1_rb1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="North" /> 

    <RadioButton 
     android:id="@+id/rg1_rb2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="South" /> 

    <RadioButton 
     android:id="@+id/rg1_rb3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Whatever" /> 
</RadioGroup> 


关键的是,你必须设置独特android:id每个RadioButton S,否则将无法正常工作!

接下来,从您的XML中找到RadioButton

RadioButton rb1, rb2, rb3; 

rb1 = (RadioButton) findViewById(R.id.rg1_rb1); 
rb2 = (RadioButton) findViewById(R.id.rg1_rb2); 
rb3 = (RadioButton) findViewById(R.id.rg1_rb3); 

最后,准备RadioButton.OnClickListener类实例并将其连接到RadioButton秒。

View.OnClickListener optionOnClickListener 
      = new View.OnClickListener() { 

    public void onClick(View v) { 
     TextView tv = (TextView) findViewById(R.id.textview); 
     String str = null; 

     // you can simply copy the string of clicked button.    
     str = ((RadioButton)v).getText().toString(); 
     tv.setText(str); 

     // to go further with check state you can manually check each radiobutton and find which one is checked. 
     if(rb1.isChecked()) { 
      // do something 
     } 
     if(rb2.isChecked()) { 
      // do something 
     } 
     if(rb3.isChecked()) { 
      // do something 
     } 
    } 
}; 

rb1.setOnClickListener(optionOnClickListener); 
rb2.setOnClickListener(optionOnClickListener); 
rb3.setOnClickListener(optionOnClickListener); 

// check rb1 by default, if you want. 
rb1.setChecked(true); 


新增:

我很抱歉,但我不明白我的答案编辑的版本,因为调用setOnClickListener()的View.OnClickLister.OnClick内()对我来说有些奇怪。
所以我回到我原来的答案。

+0

当我创建'rb1.setOnClickListener(optionOnClickListener);'时,我收到错误。它应该放置在外面吗? ? –

+0

@JohnCasey你会得到哪个错误?确保你的rb1在这一点上不是空(初始化)。 –

+0

setOnClickListener无法解析。另外,optionOnClickListener以红色下划线。 –

0

检查您的xml文件。单选按钮必须位于无线电组内才能完美工作。

<RadioGroup 
     android:id="@+id/Type" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RadioButton 
      android:id="@+id/n" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="I am a Blood Donor" /> 

     <RadioButton 
      android:id="@+id/s" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
    </RadioGroup> 

然后在你的java文件更改

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup radioGroup, int i) { 
      if (i == R.id.s) { 
       //Your action 
      } else if (i == R.id.n) { 
       //Your action 
      } 
     } 
    }); 

,将工作.. :) :)

0

好了,所以使得 'onCheckedChange' onClick事件似乎这样的伎俩。感谢所有帮助的人。

相关问题