2017-02-27 134 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事件似乎這樣的伎倆。感謝所有幫助的人。

相關問題