2016-08-17 96 views
0

所以我有一個無線電組2個單選按鈕,當用戶選擇第一個將顯示在無線電組下的文本,如果用戶選擇第二個,它將在同一區域顯示不同的消息。用Toast做這件事很容易,但我不想用Toast信息做。單擊單選按鈕時如何顯示文本?

public void onActivityCreated (Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 


    Button rb1 = (Button) getActivity().findViewById(R.id.radioButton1); 
    Button rb2 = (Button) getActivity().findViewById(R.id.radioButton2); 

    rb1.setOnClickListener(next_Listener); 
    rb2.setOnClickListener(next_Listener); 

} 

private OnClickListener next_Listener = new OnClickListener() { 

    public void onClick(View v) { 

     RadioGroup radio_grp=(RadioGroup)getActivity().findViewById(R.id.radio_grp); 
     RadioButton rb1=(RadioButton)getActivity().findViewById(R.id.radioButton1); 
     RadioButton rb2=(RadioButton)getActivity().findViewById(R.id.radioButton1); 
     if(rb1.isChecked() == true) { 

      // display text if they click 1st button 

     } 

     if (rb2.isChecked()) == true { 

      // display text if they click 2nd button 

     } 
    } 
} 

回答

1

由於單選擴展Button類,可以實現一個ClickListener

您還可以實現一個onCheckedChangedListener(),但這需要默認情況下未設置任何值。

這是由於onCheckedChangedListener()只在按鈕之間切換時觸發的事實,而且如果已經選擇了某個按鈕,則不會觸發該按鈕。

考慮你有你的TextView定義。你可以這樣做有以下:

public void onActivityCreated (Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    TextView yourTextView = (TextView) findViewById(R.id.your_text_view); 

    RadioGroup radio_grp= (RadioGroup) getView().findViewById(R.id.radio_grp); 
    RadioButton rb1 = (RadioButton) getView().findViewById(R.id.radioButton1); 
    RadioButton rb2 = (RadioButton) getView().findViewById(R.id.radioButton1); 

    View.OnClickListener button1Listener = new View.OnClickListener() { 
     public void onClick(View v) { 
      yourTextView.setText("you have touched button1"); 
     } 
    }; 

    View.OnClickListener button2Listener = new View.OnClickListener() { 
     public void onClick(View v) { 
      yourTextView.setText("you have touched button2"); 
     } 
    }; 
    rb1.setOnClickListener(button1Listener); 
    rb2.setOnClickListener(button2Listener); 
} 

@EDIT

如果RadioGroup中和相應的單選按鈕是片段的佈局裏面,你應該使用getView().findViewById()代替getActivity().findViewById()

更改代碼中找到了自己的看法對此:

RadioGroup radio_grp= (RadioGroup) getView().findViewById(R.id.radio_grp); 
RadioButton rb1 = (RadioButton) getView().findViewById(R.id.radioButton1); 
RadioButton rb2 = (RadioButton) getView().findViewById(R.id.radioButton2); 
+0

我一直在'rb1.setOnClickListener(button1Listener)獲取java.lang.NullPointerException異常; rb2.setOnClickListener(button2Listener);' –

+0

你能告訴我錯誤/ Stacktrace? 如果你在這裏得到一個NullPointerException rb1.setOnClickListener(button1Listener); ,這意味着rb1最有可能爲null,這意味着您的RadioButton rb1 =(RadioButton)getActivity(),您的 ; findViewById(R.id.radioButton1);正在返回nul ..是radioButton1佈局中的按鈕的ID? –

+0

有一個小的細節我忘了糾正你的代碼。檢查編輯。 RadioGroup和片段佈局中的相應RadioButtons? –

0

試試這個

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if(checkedId == R.id.radioButton) 
      { 
       textField.setText("first text"); 

      } 
      else 
      if (checkedId == R.id.radioButton2) { 

        textField.setText("second text"); 
      } 
     } 
    }); 
1

您需要定義OnCheckedChangeListener單選按鈕。使用這樣的

rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked()) 
        textview.setText("Radio Button 1 is checked"); 
      } 
     }); 



rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if(isChecked()) 
         textview.setText("Radio Button 2 is checked"); 
       } 
      }); 
相關問題