启用的Andr​​oid当前已禁用的微调 [英] Enable a currently disabled Spinner in Android

查看:70
本文介绍了启用的Andr​​oid当前已禁用的微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Android鬼混,并在最好的(比如,我的事实,它允许内嵌类困惑!?)我的Java知识是有限的。

I was fooling around with Android and my Java knowledge is limited at best (for instance, I'm perplexed by the fact that it allows inline classes!?).

我的问题如下:

我有一个布局,其中有三个下拉菜单。我初始化他们三个都内的onCreate()。

I have a layout where there are three dropdown menus. I initialise all three of them inside onCreate().

第一个从字符串数组取其值。第二个,然而,依赖于第一个的选择,第三个取决于第二个依次选择!

The first one takes its values from a string-array. The second one, however, depends on the choice of the first one and the third one depends on the choice of the second one in turn!

我有几个字符串数组第二微调,但我想知道什么是执行的相继启用纱厂列表的正确途径。我很想刚刚攻入并使其工作,但我不希望运行它是畸形的,不稳定的风险。

I have a few string-arrays for the second Spinner but I was wondering what would be the correct way to implement a list of successively enabled Spinners. I'm tempted to just hack into it and make it work but I don't want to run the risk of it being malformed and unstable.

因此​​,举例来说的缘故,我的情况是,如果我有href="http://developer.android.com/guide/tutorials/views/hello-spinner.html" rel="nofollow">谷歌的一个额外的微调对每个行星的卫星(然后可能对每个卫星的陨石坑)。

So for example's sakes, my case is as if I had the Google Spinner tutorial with an extra Spinner for the moons of each planet (and then maybe for craters on each of the moons).

在我的资源,我有一个arrays.xml如:

In my resources, I have an arrays.xml such as:

<resources>

<string-array name="planets">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
</string-array>
<string-array name="earth">
    <item>The Moon</item>
</string-array>
<string-array name="mars">
    <item>Deimos</item>
    <item>Phobos</item>
</string-array>
<string-array name="jupiter">
    <item>Metis</item>
    <item>Adrasthea</item>
    <item>Amalthea</item>
    <item>Thebe</item>
    <item>Io</item>
    <item>Europa</item>
    <item>Ganymede</item>
    <item>Callisto</item>
 [...] etc. etc. 
</resources>

在onCreate方法我初始化全部三个:

In the onCreate method I initialise all three:

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

  Spinner spinner1 = (Spinner) findViewById(R.id.planetSpinner);
  Spinner spinner2 = (Spinner) findViewById(R.id.moonSpinner);
  Spinner spinner3 = (Spinner) findViewById(R.id.craterSpinner);
  spinner2.setEnabled(false);
  spinner3.setEnabled(false);
  ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
        this, R.array.planets, android.R.layout.simple_spinner_item);
  adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner1.setAdapter(adapter1);
  spinner1.setOnItemSelectedListener(new MyOnPlanetSelectedListener());

}

我的监听器类

My listener class is

public class MyOnPlanetSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent,
    View view, int pos, long id) {
  Toast.makeText(parent.getContext()), "The planet is " +
      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
 }

public void onNothingSelected(AdapterView parent) {
  // Do nothing.
  }
}

所以它是。我的问题是在何处以及如何使他们一旦第一个被选中启用实施监听连续纱厂。我的第一个猜测是:

So there it is. My question is where and how to implement the listeners for the successive Spinners so that they're enabled once the first one is selected. My first guess is:

    public class MyOnPlanetSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent,
    View view, int pos, long id) {
  Toast.makeText(parent.getContext()), "The planet is " +
      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

  spinner2.setEnabled(true);

 }

public void onNothingSelected(AdapterView parent) {
  // Do nothing.
  }
}

但很明显,这是错误的,因为spinner2这里不封装。

But obviously this is wrong since spinner2 is not encapsulated here.

随后,连续微调应该选择一个字符串数组的适配器,这取决于纱厂的面前的选择是。我试图封装微调的活动课,在构造函数中,但运行前的应用程序崩溃。

Then, the successive spinners should pick a string-array for their adapters depending on what the choice of the Spinners before it are. I tried encapsulating the spinners in the activity class, in the constructor, but the application crashes before running.

非常感谢您在您的帮助。

Thank you very much in advance for your help.

推荐答案

使用了视图 onItemSelected 的参数决定哪些微调发送 onItemSelected 事件。从这个做的逻辑很简单。

Use the view parameter of onItemSelected to decide which Spinner sent the onItemSelected event. From this doing the logic is simple.

请类(活动)的spinner1 2和3成员

Make spinner1 2 and 3 member of the class (activity)

private Spinner spinner1;

然后简单比较视图参数对

if (view==this.spinner1) {
   // event came from spinner 1
   // create a new adapter, assign the new adapter to spinner 2
} else if (view==this.spinner2) {
   // event came from spinner 2
}

这篇关于启用的Andr​​oid当前已禁用的微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆