首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓微调:避免在初始化时调用onItemSelected

安卓微调:避免在初始化时调用onItemSelected
EN

Stack Overflow用户
提问于 2012-11-15 20:55:24
回答 16查看 107.1K关注 0票数 190

我创建了一个带有SpinnerTextView的安卓应用程序。我希望在TextView中显示微调器下拉列表中的选定项。我在onCreate方法中实现了微调器,因此当我运行程序时,它会在TextView中显示一个值(在从下拉列表中选择一项之前)。

我希望仅在从下拉列表中选择一个项目后,才在TextView中显示值。我该怎么做呢?

下面是我的代码:

代码语言:javascript
运行
复制
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class GPACal01Activity extends Activity implements OnItemSelectedListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.noOfSubjects);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.noofsubjects_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
        TextView textView = (TextView) findViewById(R.id.textView1);
        String str = (String) parent.getItemAtPosition(pos);
        textView.setText(str);
    }

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

    }
}
EN

回答 16

Stack Overflow用户

回答已采纳

发布于 2012-11-15 21:02:21

代码语言:javascript
运行
复制
spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.

所以第一次使用任何整数值处理这个问题

示例:最初使用int check = 0;

代码语言:javascript
运行
复制
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
   if(++check > 1) {
      TextView textView = (TextView) findViewById(R.id.textView1);
      String str = (String) parent.getItemAtPosition(pos);
      textView.setText(str);
   }
}

你可以使用布尔值,也可以通过检查当前和以前的位置来完成。See here

票数 200
EN

Stack Overflow用户

发布于 2014-08-01 07:42:10

从API3开始,您可以在带有布尔值的活动上使用onUserInteraction()来确定用户是否正在与设备交互。

http://developer.android.com/reference/android/app/Activity.html#onUserInteraction()

代码语言:javascript
运行
复制
@Override
public void onUserInteraction() {
     super.onUserInteraction();
     userIsInteracting = true;
}

作为活动中的一个字段,我有:

代码语言:javascript
运行
复制
 private boolean userIsInteracting;

最后,我的微调器:

代码语言:javascript
运行
复制
      mSpinnerView.setOnItemSelectedListener(new OnItemSelectedListener() {

           @Override
           public void onItemSelected(AdapterView<?> arg0, View view, int position, long arg3) {
                spinnerAdapter.setmPreviousSelectedIndex(position);
                if (userIsInteracting) {
                     updateGUI();
                }
           }

           @Override
           public void onNothingSelected(AdapterView<?> arg0) {

           }
      });

当您来来回回地执行该活动时,布尔值被重置为false。就像一种护身符。

票数 67
EN

Stack Overflow用户

发布于 2017-06-23 15:53:30

这对我很有效

在Android中,Spinner的初始化是有问题的,有时上面的问题可以通过这个模式来解决。

代码语言:javascript
运行
复制
Spinner.setAdapter();
Spinner.setSelected(false);  // must
Spinner.setSelection(0,true);  //must
Spinner.setonItemSelectedListener(this);

初始化微调器时,设置适配器应该是第一部分,onItemSelectedListener(这)将是最后一部分。通过上面的模式,我的OnItemSelected()在微调程序的初始化期间不会被调用

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13397933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档