下拉列表 Spinner的一个简单实例:
SpinnerActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class SpinnerActivity extends Activity implements OnClickListener{
//定义控件
private TextView show;
private Spinner spinner;
private String[] provices;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
//获取控件
spinner=(Spinner)findViewById(R.id.mySpinner);
provices=getResources().getStringArray(R.array.provice);
show=(TextView)findViewById(R.id.showSpinnerContent);
//按钮触发函数
findViewById(R.id.btnGetSpinnerContent).setOnClickListener(this);
//ArrayAdapter
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item ,
provices);
spinner.setAdapter(adapter);
//Item发放
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
show.setText(provices[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
show.setText("");
}});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnGetSpinnerContent:
int index=spinner.getSelectedItemPosition();
Toast.makeText(this,provices[index], Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mySpinner"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/showSpinnerContent"
android:layout_below="@id/mySpinner"
android:layout_marginTop="30dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/showSpinnerContent"
android:text="获取下拉框的值"
android:id="@+id/btnGetSpinnerContent"
android:layout_marginTop="30dp"/>
</RelativeLayout>
SpinnerActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class SpinnerActivity extends Activity implements OnClickListener{
//定义控件
private TextView show;
private Spinner spinner;
private String[] provices;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
//获取控件
spinner=(Spinner)findViewById(R.id.mySpinner);
provices=getResources().getStringArray(R.array.provice);
show=(TextView)findViewById(R.id.showSpinnerContent);
//按钮触发函数
findViewById(R.id.btnGetSpinnerContent).setOnClickListener(this);
//ArrayAdapter
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item ,
provices);
spinner.setAdapter(adapter);
//Item发放
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
show.setText(provices[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
show.setText("");
}});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnGetSpinnerContent:
int index=spinner.getSelectedItemPosition();
Toast.makeText(this,provices[index], Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mySpinner"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/showSpinnerContent"
android:layout_below="@id/mySpinner"
android:layout_marginTop="30dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/showSpinnerContent"
android:text="获取下拉框的值"
android:id="@+id/btnGetSpinnerContent"
android:layout_marginTop="30dp"/>
</RelativeLayout>
479

被折叠的 条评论
为什么被折叠?



