简体   繁体   English

Android Studio-如何使微调器中的第一个项目为空?

[英]Android studio - How to make the first item in the spinner to be empty?

As the topic says, how can I make the first item to be empty? 正如主题所述,如何使第一个项目为空?

As it is now, I have a value at the first place in the spinner that always is chosen from the beginning. 就像现在一样,我在微调器的第一个位置具有一个始终从一开始就选择的值。 This means that the application goes to the second activity directly, and one have no chance to select another items in the list. 这意味着该应用程序直接进入第二个活动,并且没有机会选择列表中的其他项目。

I have saved all my Strings in string.xml, in a String array. 我已将所有字符串保存在String数组中的string.xml中。 The code in MainActivity looks like this; MainActivity中的代码如下所示;

spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.country_names, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ((TextView) parent.getChildAt(0)).setText(null);
        ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
        ((TextView) parent.getChildAt(0)).setTextSize(23);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        String txtFromSpinner = mySpinner.getSelectedItem().toString();

        if (txtFromSpinner.equals("Denmark")) {
            //Go to Denmark activity
        }
    }

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

});

As you can see, the application starts with the MainActivity, but since Denmark is the first String in strings.xml, the application goes directly to the "Denmark activity". 如您所见,该应用程序从MainActivity开始,但是由于丹麦是strings.xml中的第一个String,因此该应用程序直接进入“丹麦活动”。

Add an empty string item on your string.xml, somthing like this: 在您的string.xml上添加一个空字符串项目,如下所示:

<string-array name="country_names">
        <item></item>
        <item>Denmark</item>

    </string-array>

Update2 更新2

Or you can do this setSelection(position, false); 或者您可以执行此setSelection(position, false); in the initial selection before setOnItemSelectedListener(listener) setOnItemSelectedListener(listener)之前的初始选择中

So update your code to this: 因此,将您的代码更新为:

   spinner = (Spinner) findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(this, R.array.country_names, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

    spinner.setAdapter(adapter);
    setSelection(0, false); 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setText(null);
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            ((TextView) parent.getChildAt(0)).setTextSize(23);

            Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
            String txtFromSpinner = mySpinner.getSelectedItem().toString();

            if (txtFromSpinner.equals("Denmark")) {
                //Go to Denmark activity
            }
        }

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

        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM