事件(3)

下拉框:OnItemSelectedListener

  sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override    

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

      TextView tv1=(TextView)view;
      String str=tv1.getText().toString();
      tv.setText(str);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {


    }
  });

 1 package com.hh;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.AdapterView;
 7 import android.widget.Spinner;
 8 import android.widget.TextView;
 9 import android.widget.AdapterView.OnItemSelectedListener;
10 
11 public class Click extends Activity{
12     private TextView tv;        //定义信息显示组件
13     private Spinner sp;
14         
15         sp=(Spinner) findViewById(R.id.spe1);
16         sp.setOnItemSelectedListener(new OnItemSelectedListener() {
17 
18             @Override
19             public void onItemSelected(AdapterView<?> arg0, View view,
20                     int arg2, long arg3) {
21                 TextView tv1=(TextView)view;
22                 String str=tv1.getText().toString();
23                 tv.setText(str);
24             }
25 
26             @Override
27             public void onNothingSelected(AdapterView<?> arg0) {
28                 
29             }
30         });
31 }
代码示例
 1  <TextView
 2           android:id="@+id/tve1"
 3         android:layout_width="fill_parent" 
 4         android:layout_height="wrap_content" 
 5            android:background="#FF0000"
 6       />
 7       
 8       <Spinner
 9           android:id="@+id/spe1"
10         android:layout_width="fill_parent" 
11         android:layout_height="wrap_content" 
12         android:entries="@array/citys"
13       />
xml配置文件

原文地址:https://www.cnblogs.com/yang82/p/6964663.html