进入活动后会自动选择微调项目。我该如何避免这种情况?

发布于 2024-10-24 13:18:06 字数 103 浏览 7 评论 0原文

我的 Android 应用程序中有一个微调器,其 onItemSelected() 事件在进入 Activity 时自动触发。

我该如何避免这种情况?

I have a spinner in my Android app, and its onItemSelected() event automatically gets triggered upon entering the activity.

How do I avoid this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

傲鸠 2024-10-31 13:18:06

我们可以使用一个标志,并在真正触摸旋转器时启用它。

private boolean isSpinnerTouched = false; 

spinner.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isSpinnerTouched = true;
                return false;
            }
        });
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapter, View arg1,
                        int arg2, long arg3) {
                    if (!isSpinnerTouched) return;
                    // do what you want 
                    }
        });

We can use a flag, and just enable it when the spinner is really touched.

private boolean isSpinnerTouched = false; 

spinner.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isSpinnerTouched = true;
                return false;
            }
        });
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapter, View arg1,
                        int arg2, long arg3) {
                    if (!isSpinnerTouched) return;
                    // do what you want 
                    }
        });
究竟谁懂我的在乎 2024-10-31 13:18:06

要添加 Jerry Abraham,您应该在启用 setOnItemSelectedListener 之前清除选择

  Spinner mSpinner=(Spinner)findViewById(R.id.mySpinner);
  int initialSelectedPosition=mSpinner.getSelectedItemPosition();
  mSpinner.setSelection(initialSelectedPosition, false); //clear selection
  mSpinner.setOnItemSelectedListener(this); //set listener after clearing section

To add on Jerry Abraham, You should clear selection before enabling setOnItemSelectedListener

  Spinner mSpinner=(Spinner)findViewById(R.id.mySpinner);
  int initialSelectedPosition=mSpinner.getSelectedItemPosition();
  mSpinner.setSelection(initialSelectedPosition, false); //clear selection
  mSpinner.setOnItemSelectedListener(this); //set listener after clearing section
浮生未歇 2024-10-31 13:18:06

我已经解决了这个问题,
您可以通过不为微调器设置任何默认值来避免此问题

        int initialposition=spinner.getSelectedItemPosition();
        spinner.setSelection(initialposition, false);

这将避免进入 onItemSelected()

I have solved this issue,
You can avoid this issue by not setting any default values to the spinner

        int initialposition=spinner.getSelectedItemPosition();
        spinner.setSelection(initialposition, false);

This will avoid entering into onItemSelected()

伪心 2024-10-31 13:18:06

没有任何办法可以避免这种情况。

您可以添加一些标志,指示您的应用程序已准备就绪,并在 onItemSelected() 方法中使用它来决定在每种情况下要做什么。

There are no any way to avoid this.

You may add some flag, indicating readiness of your application and use it in your onItemSelected() method to decide, what to do in each case.

染年凉城似染瑾 2024-10-31 13:18:06

那么,您可以向初始适配器添加一个虚拟选择,并忽略 setOnItemSelectedListener 中的位置号。它不漂亮,但很有效。请参阅此代码以设置阵列适配器的项目。

List<String> names = new ArrayList<String>();
names.add("");
names.addAll(realValues);

然后在您的 setOnItemSelectedListener 中您可以执行以下操作:

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        if (position > 0)
        {
            String name = names.get(position - 1);
        }
        else
        {
            Log.d(TAG, "selected nothing or perhaps the dummy value");
        }
    }

Well, you can add a dummy selection to the initial adapter, and ignore position number in the setOnItemSelectedListener. It's not pretty but it works. See this code for setting up the items for an array adapter.

List<String> names = new ArrayList<String>();
names.add("");
names.addAll(realValues);

Then in your setOnItemSelectedListener you can do this:

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        if (position > 0)
        {
            String name = names.get(position - 1);
        }
        else
        {
            Log.d(TAG, "selected nothing or perhaps the dummy value");
        }
    }
山有枢 2024-10-31 13:18:06

我找到了这个问题的解决方案并将其发布在这里(带有代码示例):

Spinner onItemSelected() 在不应该执行的时候执行

I have found a solution for this problem and posted it here (with code sample):

Spinner onItemSelected() executes when it is not suppose to

夏有森光若流苏 2024-10-31 13:18:06

简单又容易,就是这样……
使用布尔值进行验证,看看是否是第一次...

Spinner mySpinner = (Spinner)findViewById(R.id.spinner_xml_pro);
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(isSpinnerInitial){ // globar var boolean isSpinnerInitial = false;
                 //do something
               }else
                    isSpinnerInitial=true;
            }

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

            }
        });

检查此 with spinner.post(new Runnable()...)
或其他
我的来源

Simple and easy is this...
validate with a boolean to see if is first time...

Spinner mySpinner = (Spinner)findViewById(R.id.spinner_xml_pro);
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(isSpinnerInitial){ // globar var boolean isSpinnerInitial = false;
                 //do something
               }else
                    isSpinnerInitial=true;
            }

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

            }
        });

Check this with spinner.post(new Runnable()...)
or this other my source

黑寡妇 2024-10-31 13:18:06

我认为您可以使用微调器位置,我认为这是更好的方法。
创建一个全局变量来存储微调器位置,在 onItemSelected 方法中提供位置,您可以比较它们,如果它们相同,则不执行操作。

    private int spinnerPosition; \\ Global variable

     mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
               if(spinnerPosition != position){
               // Do whatever you like

               // Do not forget to save the new position
                spinnerPosition = position;
               }

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

            }
        });

I think that you can use spinner position which is a better approach in my opinion.
Create a global variable where you store the spinner position, in onItemSelected method the position is provided you can compare them, if they are the same do not make an action.

    private int spinnerPosition; \\ Global variable

     mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
               if(spinnerPosition != position){
               // Do whatever you like

               // Do not forget to save the new position
                spinnerPosition = position;
               }

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

            }
        });
你与清晨阳光 2024-10-31 13:18:06

您可以通过忽略第一次点击来避免它,

private boolean isSpinnerInitial = true; //As global variable

public void onItemSelected(xxx xxx, xxx xxx, xxx xxx, xxx xxx) {
    if(isSpinnerInitial) {
        isSpinnerInitial = false;
        return;
    }
    // Write your code here
}

You can avoid it by ignoring the first click by,

private boolean isSpinnerInitial = true; //As global variable

public void onItemSelected(xxx xxx, xxx xxx, xxx xxx, xxx xxx) {
    if(isSpinnerInitial) {
        isSpinnerInitial = false;
        return;
    }
    // Write your code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文