如何将默认文本设置为Spinner

时间:2013-11-13 05:11:58

标签: android spinner

我想设置微调器以显示名称country,即使选择了任何其他列表也是如此。我使用了一个textview来显示Spinner List的选定项目。当我使用setselection方法将微调器标题设置为Country时,EditText最终也会更改。我经历了各种有关此问题的回答,但无法找到合适的回复

我正在附上我的代码

MainActivity.java

package com.example.spinner;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
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 MainActivity extends Activity {

    Spinner sp;
    TextView t;
    String[] country;
    int sp_position;
    String selected;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String myString = "Country"; 

        sp = (Spinner)findViewById(R.id.spinner1);
        t = (TextView)findViewById(R.id.textView1);
        country = getResources().getStringArray(R.array.spinner);
        ArrayAdapter<String> ad = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,country);   
        sp_position = ad.getPosition(myString);
        sp.setAdapter(ad);
        ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                selected = sp.getSelectedItem().toString();
                System.out.println(selected);
                setid();

            }

            private void setid() {
                sp.setSelection(sp_position);


            }

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

            }
        });

        t.setText(selected);

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="69dp"
        android:maxLines="4"
        android:hint="Address" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="72dp" 
        />

</RelativeLayout>

我的STRINGS.XML

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Spinner</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string-array name="spinner">
        <item>Country</item>
        <item>India</item>
        <item>Russia</item>
        <item>USA</item>
        <item>France</item>
        <item>United Kingdom</item>
    </string-array>
<string name ="Country">Country</string>
</resources>

我的要求是在微调框中选择的任何内容,Textview应显示所选的项目,但微调框应始终显示第一个项目或在我的情况下显示国家

11 个答案:

答案 0 :(得分:12)

使用此代码

声明

String selected, spinner_item;

微调代码

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            selected = sp.getSelectedItem().toString();
            if (!selected.equals("Country"))
                spinner_item = selected;
            System.out.println(selected);

            setid();
        }

        private void setid() {
            sp.setSelection(sp_position);
            t.setText(spinner_item);
        }

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

        }
    });

答案 1 :(得分:8)

spinner.setPrompt("Pick One");

答案 2 :(得分:2)

在微调器中使用android:prompt="@string/select" ....

答案 3 :(得分:2)

这样做。 将textView setSelection设置为微调器列表的第0个位置

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {

        selected = sp.getSelectedItem().toString();
        System.out.println(selected);
        t.setText(selected);
        sp.setSelection(0);
    }
}

答案 4 :(得分:2)

您只需要3行代码:

(1)声明一个全局变量,用于保存和加载微调器项ID。通过全局,我的意思是在公共类MainActivity扩展Activity {&#34;。

之后立即宣布它
int mySpinner_selectedId; 

(2)然后,在用户做出选择之后添加以下代码。一个好的位置是与您的微调器一起出现的按钮。

mySpinner_selectedId = mySpinner.getSelectedItemPosition();

(3)最后,在&#34; mySpinner.setAdapter(adapter);&#34;之后添加以下代码。加载最后选择的项目。

mySpinner.setSelection(mySpinner_selectedId);

你最受欢迎。

答案 5 :(得分:1)

试试这种方式。

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        //selected = sp.getSelectedItem().toString();
        //System.out.println(selected);
        if(arg2!=0)
           t.setText(sp.getSelectedItem().toString());
        sp.setSelection(0);
    }
}

我希望这会对你有所帮助。让我知道发生什么事。谢谢。

答案 6 :(得分:1)

试试这个:

    public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {

           spinner.setSelection(pos);

    }

答案 7 :(得分:0)

试试这个

Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            if (position == 0) {
                txtSpinner1.setHint("Select Country");
            } else {
                txtSpinner1.setText(CountryList.get(position));
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            txtSpinner1.setHint("Select Country");
        }

    });

答案 8 :(得分:0)

onItemSelected方法中添加此代码sp.setSelection(0);

答案 9 :(得分:0)

我以一种简单的方式完成了这项工作

  1.   

    在适配器第一个或结束位置添加默认文本(在我的情况下结束)和将此项目设置为默认选择。 ----同时获取默认选择位置,以便在setOnItemSelectedListener()中不执行任何操作。

    taskName.add("one");
    taskName.add("two");
    taskName.add("select a task");
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, taskName);
    binding.spnTask.setAdapter(adapter);
    binding.spnTask.setSelection(adapter.getPosition("select a task name"));
    final int defaultPosition = binding.spnTask.getSelectedItemPosition();
    
  2.   

    检查所选位置是默认文本位置

      binding.spnTask.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
            if (position == defaultPosition) {
                Toast.makeText(this, "No Action", Toast.LENGTH_SHORT).show();
            }else{  Toast.makeText(this, "Your Action" , Toast.LENGTH_SHORT).show();   }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    

答案 10 :(得分:0)

mySpinner = (Spinner) findViewById(R.id.spinner);
        mySpinner.setPrompt("Categorie");
        final ArrayList<String> strDeviceArr = new ArrayList<>();

        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String valueSelected = mySpinner.getSelectedItem().toString();
                if(!valueSelected.equals("Categorie"))
                    Toast.makeText(MenuActivity.this, ""+valueSelected, Toast.LENGTH_SHORT).show();

            }

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

            }
        });