即使选择了任何其他列表,我也想设置微调器以显示名称国家。我使用了一个 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 应显示所选项目,但微调器应始终显示第一个项目,或者在我的情况下为 Country