码迷,mamicode.com
首页 > 移动开发 > 详细

android下拉框

时间:2014-09-07 13:29:25      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:android   http   os   io   ar   数据   cti   sp   on   

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"
    android:orientation="vertical" >
   
<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/spinner"
    android:layout_marginTop="20dp"
    android:id="@+id/selectSpinnerValue"
    android:text="获取下拉框的值"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/selectSpinnerValue"
    android:layout_marginTop="20dp"
    android:id="@+id/showSpinnerValue"
    />
</RelativeLayout>

Activity:

public class SpinnerActivity extends Activity implements OnClickListener{
   private String[] food={"炒面","凉面","炸酱面","打卤面","手擀面"};
   private TextView showSpinnerValue;
   private Spinner spinner;
   private String[] tel;
 public  void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.spinner);
        spinner=(Spinner)findViewById(R.id.spinner);
        showSpinnerValue=(TextView)findViewById(R.id.showSpinnerValue);
        findViewById(R.id.selectSpinnerValue).setOnClickListener(this);
        //从资源读取数据内容
        tel=getResources().getStringArray(R.array.phonenumber);
        //定义适配器
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(SpinnerActivity.this,
          android.R.layout.simple_spinner_dropdown_item, tel);
        //给控件设置适配器
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
    showSpinnerValue.setText(food[arg2]);
   }
   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
                showSpinnerValue.setText("");
   }
         
  });
 }

 @Override
 public void onClick(View v) {
  switch(v.getId()){
  case R.id.selectSpinnerValue:
   int index=spinner.getSelectedItemPosition();
   Toast.makeText(this, food[index],Toast.LENGTH_LONG ).show();
   break;
   default:
    break;
  }
  
 }

}

android下拉框

标签:android   http   os   io   ar   数据   cti   sp   on   

原文地址:http://www.cnblogs.com/fanweichao/p/3960335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!