【问题标题】:Why i cannot select first choice of the spinner?为什么我不能选择微调器的首选?
【发布时间】:2014-10-20 15:27:33
【问题描述】:

我创建了一个微调器,其中第一个选择显示为默认值。但是如果我点击它就不起作用了。

protected boolean inhibit_spinner = true;

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();

    if (inhibit_spinner) {
        inhibit_spinner = false;
    }else {
        Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Selection.this, MainActivity.class);
        intent.putExtra("selection", item);
        startActivity(intent);
        finish();
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

如果我单击微调器并选择它引发活动的任何其他选项,但如果我单击它并选择默认值(第一选择)它不会引发活动。为什么?

【问题讨论】:

    标签: android list drop-down-menu spinner default


    【解决方案1】:

    默认选择第一个选项(位置 0)。所以 onItemSelected 没有被调用,因为位置没有改变。

    尝试添加一个像“选择一个”这样的元素作为第一个选项以避免这种行为。

    【讨论】:

      【解决方案2】:

      只有当用户在微调器中选择的项目与当前选择的项目不同时,才会调用 onItemSelected 函数。

      例如:

      如果您的微调器项目是 item1、item2、item3 和 item4。

      • 选择 item4 ,然后调用 onItemSelected 函数,位置为 3。
      • 再次选择item4,但这一次不会调用onItemSelected函数。
      • 选择 item0 ,则调用 onItemSelected 函数,位置为 0。
      • 再次选择item0,但这一次不会调用onItemSelected函数。

      我相信您需要稍微改变一下应用程序流程。无需转到选择微调器项目的下一个活动,而是向用户提供一个按钮。用户可以使用微调器选择他想要的选项。当他单击按钮时,使用函数“spinner.getSelectedItem()”从微调器中获取当前选定的项目并将其传递给下一个活动。

      如果这不能解决您的问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 2019-09-22
        相关资源
        最近更新 更多