如何设置默认文本到微调

即使select了其他列表,我也要设置微调器显示姓名的国家。 我已经使用了一个显示Spinner列表中所选项目的TextView。 正如我使用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> 

我的要求是什么在微调selectTextview应该显示选定的项目,但微调应始终显示第一项或在我的情况下国家

Solutions Collecting From Web of "如何设置默认文本到微调"

使用这个代码

宣言

 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) { } }); 
 spinner.setPrompt("Pick One"); 

这样做。 将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); } } 

你只需要3行代码:

(1)声明一个用于保存和加载微调项目ID的全局variables。 就全局而言,我的意思是在“public class MainActivity extends Activity {”之后声明它。

 int mySpinner_selectedId; 

(2)然后,在用户做出select之后添加下面的代码。 一个好的位置将是一个button,与你的微调一起出现。

 mySpinner_selectedId = mySpinner.getSelectedItemPosition(); 

(3)最后,在“mySpinner.setAdapter(adapter);”之后添加以下代码 加载最后select的项目。

 mySpinner.setSelection(mySpinner_selectedId); 

你最受欢迎。

使用android:prompt="@string/select" in spinner ….

试试这个方法。

 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); } } 

我希望这能帮到您。 让我知道发生什么事。 谢谢。

尝试这个:

  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { spinner.setSelection(pos); } 

尝试这个

 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"); } }); 

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