Android学习笔记(一) 下拉列表Spinner控件

    技术2022-06-09  41

    一、在项目res/layout/main.xml添加一个Button,点击Button跳转到SpinnerActivity

     

    <Button android:id="@+id/spinner_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GoToSpinnerActivity" />

     

    二、在MainActivity添加Button侦听

     

    //找到main.xml中的Button private void find_and_modify_spinner_button(){ Button spinner_button=(Button)findViewById(R.id.spinner_button); spinner_button.setOnClickListener(spinner_button_listener); } //Button事件 private Button.OnClickListener spinner_button_listener=new Button.OnClickListener(){ public void onClick(View v) { Intent intent=new Intent(); intent.setClass(widget.this,SpinnerActivity.class); startActivity(intent); } };  

     

    三、在AndroidManifest.xml声明SpinnerActivity

     

    <activity android:name="SpinnerActivity"></activity>

     

    四、新建类 SpinnerActivity.java,使用下拉列表Spinner控件,两种读取数据方法

     

    SpinnerActivity.java代码:

     

    public class SpinnerActivity extends Activity { Spinner spinner_1; Spinner spinner_2; private List<String> words; private ArrayAdapter<String> allwords; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); setTitle("SpinnerActivity"); find_and_modify_view(); } private static final String [] str=new String []{"fuck","shit","cao","del"}; private void find_and_modify_view() { words=new ArrayList<String>(); for(int i=0;i<str.length;i++) { words.add(str[i]); } spinner_1=(Spinner)findViewById(R.id.spinner_1);//↓不同于ArrayAdapter<CharSequence>的使用方法 allwords=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,words); allwords.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_1.setAdapter(allwords); spinner_2=(Spinner)findViewById(R.id.spinner_2);//↓不同于ArrayAdapter<String>的使用方法 ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.arr_words,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_2.setAdapter(adapter); }

     

    spinner.xml代码:

     

    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="spinner_1"/> <Spinner android:id="@+id/spinner_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="spinner_2"/> <Spinner android:id="@+id/spinner_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" />

     

    arrays.xml代码:(res/value下新建)

     

    <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="arr_words"> <item>fuck01</item> <item>shit02</item> <item>cao01</item> <item>del01</item> </string-array> </resources>

     

    完毕,效果图:

     


    最新回复(0)