TextView+Spinner的使用


arrayxml



   
        四次实验报告
        四篇阶段性文档
        2000行代码
        小农夫
   


布局xml


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


            android:id="@+id/textView2"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="作业" />


            android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/languages" />


            android:id="@+id/button3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="next" />



public class TwoActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ativity_two);
        Spinner spinner =(Spinner)findViewById(R.id.spinner);
        // 建立数据源
        
String[] mItems = getResources().getStringArray(R.array.languages);
// 建立Adapter并且绑定数据源
        
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item, mItems);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
        
spinner .setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int pos, long id) {

                String[] languages = getResources().getStringArray(R.array.languages);
                Toast.makeText(TwoActivity.this, "你点击的是:"+languages[pos], Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView parent) {
                // Another interface callback
            
}
        });
    }
}

 TextView+Spinner的使用_第1张图片

你可能感兴趣的:(TextView+Spinner的使用)