gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:14:23 27 4
gpt4 key购买 nike

我是 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(
));
}
}

最佳答案

在 XML 文件中,将 radiogroupid 更改为 radiogroup1

关于java - 在单击事件上创建单选按钮以更改 java 中的微调器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8822687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com