为什么微调监听器中需要onNothingSelected方法? [英] Why is onNothingSelected method needed in spinner listener?

查看:360
本文介绍了为什么微调监听器中需要onNothingSelected方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以英语为母语的人,所以我对你们不好的英语能力表示歉意.

native English speaker, so I'd say sorry about my bad English skills to you guys.

我从5周前开始学习Android.我试图实现一个微调器,导师问为什么需要onNothingSelected方法.我无话可说

I've been studing Android since 5 weeks ago. I tried to implement a spinner and my mentor asked why the onNothingSelected method is needed. I had nothing to say.

那么,为什么我需要那种方法?你可以回复吗?

So, why do I need that method?? Can you reply it?

以下代码是我的微调器.它可以正确执行我的预期.

Following code is my spinner. It does correctly what I intended.

public class SpinnerViewPractice extends Activity {
private Spinner spinner;
private String spinner_value = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.spinnerviewpractice);

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

    String[] str = {"","good", "dislike", "like", "hate", "moderate"};
    spinner.setPrompt("Set Text");
    ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, str); 
    spinner.setAdapter(list);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            TextView tv = (TextView)arg1;
            spinner_value = tv.getText().toString();
            if(spinner_value.length() == 0)
            {
                spinner_value = "Nothing";                  
            }
            Toast.makeText(SpinnerViewPractice.this, spinner_value, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Toast.makeText(SpinnerViewPractice.this, "NothingSelected", Toast.LENGTH_SHORT).show();
        }           
    });
}

}

推荐答案

如文档所述:

当选择从该视图中消失要被调用的回调方法.例如,当触摸被激活或适配器变空时,该选择可能会消失.

Callback method to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty.

这意味着只要将当前选定的项目从可用项目列表中删除,就会调用该方法.正如文档所描述的,这可能在不同的情况下发生,但是通常如果修改了适配器,使得当前选择的项目不再可用,则将调用该方法.

This means that the method is called whenever the currently selected item is removed from the list of available items. As the doc describes, this can occur under different circumstances, but generally if the adapter is modified such that the currently selected item is no longer available then the method will be called.

可以使用此方法,以便在上一个项目不再可用的情况下设置将要选择的项目.这不是让微调框自动选择列表中的下一个项目.

This method may be used so that you can set which item will be selected given that the previous item is no longer available. This is instead of letting the spinner automatically select the next item in the list.

这篇关于为什么微调监听器中需要onNothingSelected方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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