在click事件上创建on单选按钮以更改java中的微调器数组

时间:2012-01-11 16:16:16

标签: java android

我是Android编程的新手。

我想要两个单选按钮,点击它会改变微调器阵列。

我知道这听起来很简单。我已经完成了所有工作,但我不确定如何实现onclickchange来实现这一点。我附上了代码。

我正在努力探索如何设置此监听器。此外,我没有错误,并且微调器工作,只是无法让它更改数组。

非常感谢任何指导。 欢呼声,

我试图用听众创建它,但它不起作用,任何建议都表示赞赏。

XML

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

    <RadioButton
        android:id="@+id/radiobutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/radio1" />

    <RadioButton
        android:id="@+id/radiobutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio2" />
</RadioGroup>

的java

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.canprovselect);


    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup1);

    int checkedRadioButton = radioGroup.getCheckedRadioButtonId();

    Spinner spinner = (Spinner) findViewById(R.id.spinner);     
    ArrayAdapter<CharSequence> adapter = null;

    String radioButtonSelected = "";
    switch (checkedRadioButton) {
    case R.id.radiobutton3 : radioButtonSelected = "radiobutton1";

    adapter = ArrayAdapter.createFromResource(
            this, R.array.prov_array, android.R.layout.simple_spinner_item);     
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelected(
            ));

    break;
    case R.id.radiobutton4 : radioButtonSelected = "radiobutton2";
    adapter = ArrayAdapter.createFromResource(             
            this, R.array.prov1_array, android.R.layout.simple_spinner_item);     
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelected(
            ));

    break;
    }

我对听众的尝试

public class CanProvselect extends Activity {

RadioButton myOption1, myOption2;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.canprovselect);
    myOption1 = (RadioButton)findViewById(R.id.radio_1); 
    myOption2 = (RadioButton)findViewById(R.id.radio_2); 
    myOption1.setOnClickListener(myOptionOnClickListener); 
    myOption2.setOnClickListener(myOptionOnClickListener); 

    OnClickListener myOptionOnClickListener = null;

    Spinner spinner = (Spinner) findViewById(R.id.spinner);     
    ArrayAdapter<CharSequence> adapter = null;


}

RadioButton.OnClickListener myOptionOnClickListener = 
        new RadioButton.OnClickListener() 
{

    @Override 
public void onClick(View v) { 
        // TODO Auto-generated method stub 
        //switch (checkedRadioButton) {     
        myOption1 : radioButtonSelected = "radio_1";      
        adapter = ArrayAdapter.createFromResource(             
                this, R.array.prov_array, android.R.layout.simple_spinner_item);          
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);          
        spinner.setAdapter(adapter);     
        spinner.setOnItemSelectedListener(new OnItemSelected(             
                ));

        myOption2 : radioButtonSelected = "radio_2";     
        adapter = ArrayAdapter.createFromResource(                          
                this, R.array.prov1_array, android.R.layout.simple_spinner_item);          
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);          
        spinner.setAdapter(adapter);     
        spinner.setOnItemSelectedListener(new OnItemSelected(             
        )); 
    }
}

}

2 个答案:

答案 0 :(得分:2)

在XML文件中,将id的{​​{1}}更改为radiogroup

答案 1 :(得分:0)

您需要设置radioGroup.setOnClickListener(yourListenerClass)。这是一个例子。Radio button onclick event

因为有onast方法的onClick方法,请将其替换为listview人口代码。