Android 单微调器,多个数组
我无法在任何地方找到这个问题的答案(很可能我没有搜索正确)。在 Android 中,我试图在我的应用程序中创建一个微调器,根据选择的单选按钮从多个数组中提取。如果选择单选按钮 1,我希望微调器使用数组 1,如果选择单选按钮 2,我希望微调器使用数组 2。这是我到目前为止所做的,但它不起作用。每次我使用此代码单击应用程序中的选项卡时,我的应用程序都会强制关闭。
public class This_Activity extends Activity {
/** Called when the activity is first created. */
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_2_layout);
rb1=(RadioButton)findViewById(R.id.Radio1);
rb2=(RadioButton)findViewById(R.id.Radio2);
rb3=(RadioButton)findViewById(R.id.Radio3);}{
if(rb1.isChecked() == true){
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
if(rb2.isChecked() == true){
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
if(rb3.isChecked() == true){
Spinner spinner3 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array3, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter);
}
}
}
I haven't been able to find the answer to this anywhere (it's quite possible I'm not searching right). In Android I'm trying to make a single spinner in my app pull from multiple arrays depending on which radio button is selected. If radio button 1 is selected I want the spinner to use array 1, if radio button 2 is selected I want the spinner to use array 2. Here is what I have so far, but it doesn't work. Everytime I click on the tab in my app with this code my app force closes.
public class This_Activity extends Activity {
/** Called when the activity is first created. */
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_2_layout);
rb1=(RadioButton)findViewById(R.id.Radio1);
rb2=(RadioButton)findViewById(R.id.Radio2);
rb3=(RadioButton)findViewById(R.id.Radio3);}{
if(rb1.isChecked() == true){
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
if(rb2.isChecked() == true){
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
if(rb3.isChecked() == true){
Spinner spinner3 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array3, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到错误消息,则很难诊断问题。您可以编辑您的问题并包含这些内容吗?
然而,我注意到的一件事是,您在每种情况下都创建了不同的旋转器。尝试使用
上面的 if 语句。那么 if 语句的主体看起来就像
您可能还希望将侦听器连接到单选按钮,以便在用户单击它们时自动更改微调器,因为现在这只会在构造函数中发生。
将单选按钮放在 RadioGroup 中可能更容易OnCheckChangedListener ,因此您只需要一个侦听器来处理所有 3 个按钮。
It is difficult to diagnose the problem without the error messages you are seeing. Could you edit your question and include those?
However, one thing I am noticing is that you are creating different spinners in each of those cases. Try using
above your if statements. Then the body of your if statements will look like
you also probably want to hook up listeners to your radio buttons to change the spinner automatically when the user clicks on them, because as it is now this will only happen in the constructor.
it is probably easier to put your radio buttons in a RadioGroup and put the OnCheckChangedListener on that, so you only need one listener for all 3 buttons.