Activity之常见控件(二)
上一篇文章中,记录一些在activity常见的控件,今天再记录也是很常见的控件,但其用法相对来说,要更复杂一些,直接来例子:
1. ListView
ListView一看名字就知道了,用于在activity中展示一个list,例如我们要查询出所有的用户并在activity中展示出来,那么此时ListView就派上用场了;首先看Activity的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package cn.bridgeli.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<Map<String, String>> list = new ArrayList<Map<String,String>>(); Map<String, String> map1 = new HashMap<String, String>(); Map<String, String> map2 = new HashMap<String, String>(); Map<String, String> map3 = new HashMap<String, String>(); map1.put( "username" , "test1" ); map1.put( "user_ip" , "192.168.0.1" ); map2.put( "username" , "test2" ); map2.put( "user_ip" , "192.168.0.2" ); map3.put( "username" , "test3" ); map3.put( "user_ip" , "192.168.0.3" ); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.item, new String[]{ "username" , "user_ip" }, new int[]{R.id.username, R.id.user_ip}); setListAdapter(listAdapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); System.out.println( "position === " + position); System.out.println( "id === " + id); } } |
这里面用到了两个布局文件,一个是主布局文件activity_main.xml,比较简单,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo.MainActivity" > <LinearLayout android:id= "@+id/listLinearLayout" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <ListView android:id= "@id/android:list" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" android:drawSelectorOnTop= "false" /> </LinearLayout> </LinearLayout> |
另一个是每一个条目的布局文件:item.xml,其实也很简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo.MainActivity" android:paddingLeft= "10dip" android:paddingRight= "10dip" android:paddingTop= "1dip" android:paddingBottom= "1dip" > <TextView android:id= "@+id/username" android:layout_width= "180dip" android:layout_height= "30dip" android:textSize= "10pt" android:singleLine= "true" /> <TextView android:id= "@+id/user_ip" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:textSize= "10pt" android:gravity= "right" /> </LinearLayout> |
需要说明的是,①. 主布局文件中,ListView的ID是Android系统提供的;
②. 每一个条目的布局文件和具体展示的数据是相对应的,所以本例中每一个条目有两列。
2. ExpandableListActivity
这个则更复杂一些,他不仅仅是直接展示出来,更需要对展示的数据进行分组,如果系统要求不仅把所有的用户都展示出来,更要求把同一个级别的用户放到同一个组里面,那么ExpandableListActivity就派上用了,先看activity代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package cn.bridgeli.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ExpandableListActivity; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.SimpleExpandableListAdapter; public class MainActivity2 extends ExpandableListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); List<Map<String, String>> groups = new ArrayList<Map<String,String>>(); Map<String, String> group1 = new HashMap<String, String>(); group1.put( "group" , "group1" ); Map<String, String> group2 = new HashMap<String, String>(); group1.put( "group" , "group2" ); groups.add(group1); groups.add(group2); List<Map<String, String>> child1 = new ArrayList<Map<String,String>>(); Map<String, String> child1Data1 = new HashMap<String, String>(); child1Data1.put( "child1" , "child1Data1" ); Map<String, String> child1Data2 = new HashMap<String, String>(); child1Data1.put( "child1" , "child1Data2" ); child1.add(child1Data1); child1.add(child1Data2); List<Map<String, String>> child2 = new ArrayList<Map<String,String>>(); Map<String, String> child2Data1 = new HashMap<String, String>(); child1Data1.put( "child2" , "child2Data1" ); Map<String, String> child2Data2 = new HashMap<String, String>(); child1Data1.put( "child2" , "child2Data2" ); child2.add(child2Data1); child2.add(child2Data2); List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>(); childs.add(child1); childs.add(child2); SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, groups, R.layout.group, new String[]{ "group" }, new int[]{R.id.groupTo}, childs, R.layout.child, new String[]{ "child" }, new int[]{R.id.childTo}); setListAdapter(adapter); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { return super.onChildClick(parent, v, groupPosition, childPosition, id); } } |
这个里面用到了三个布局文件,分别是主布局文件:activity_main3.xml,一级目录的布局文件:group.xml和二级目录的布局文件:child.xml,代码分别如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo.MainActivity" > <ExpandableListView android:id= "@id/android:list" android:layout_width= "match_parent" android:layout_height= "match_parent" android:drawSelectorOnTop= "false" /> <TextView android:id= "@id/android:empty" android:layout_width= "match_parent" android:layout_height= "match_parent" android:text= "no data" /> </LinearLayout> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <TextView android:id= "@+id/groupTo" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:paddingLeft= "10dip" android:paddingRight= "10dip" android:paddingTop= "1dip" android:paddingBottom= "1dip" android:textSize= "10pt" android:singleLine= "true" /> </LinearLayout> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <TextView android:id= "@+id/childTo" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:paddingLeft= "10dip" android:paddingRight= "10dip" android:paddingTop= "1dip" android:paddingBottom= "1dip" android:textSize= "10pt" android:singleLine= "true" /> </LinearLayout> |
3. Spinner
这个在开发中,也很常见,老夫理解其实就是我们在web中的下拉框而已,其中下拉的数据实现有两种,第一种:直接在strings.xml中写死
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package cn.bridgeli.demo2; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.spinnerId); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R. array .planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setPrompt( "test" ); spinner.setOnItemSelectedListener( new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { String selectId = adapterView.getItemAtPosition(position).toString(); System.out.println(selectId); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } } |
布局文件如下:
1 2 3 4 5 6 7 8 9 10 11 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo2.MainActivity" > <Spinner android:id= "@+id/spinnerId" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </LinearLayout> |
从代码中我们看到,有一个数据源,就是提供给我们下拉选择有哪些,就是strings.xml:
1 2 3 4 5 6 7 8 9 | <?xml version= "1.0" encoding= "utf-8" ?> <resources> <string- array name= "planets_array" > <item >Mercury</item> <item >Venus</item> <item >Earth</item> </string- array > </resources> |
这个其实用途算是一般吧,因为有些时候我们并不知道下拉的是那些数据,是我们从数据库中根据不同的情况读取出来的,这个也很简单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package cn.bridgeli.demo2; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.spinnerId); List<String> list = new ArrayList<String>(); list.add( "test1" ); list.add( "test2" ); list.add( "test3" ); ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, R.layout.item, R.id.textView, list); spinner.setAdapter(adapter); spinner.setPrompt( "test" ); spinner.setOnItemSelectedListener( new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { String selectId = adapterView.getItemAtPosition(position).toString(); System.out.println(selectId); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } } |
从代码里面我们可以看到,这里面多了一个每一个条目的布局文件,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo2.MainActivity" > <TextView android:id= "@+id/textView" android:layout_width= "fill_parent" android:layout_height= "match_parent" /> </LinearLayout> |
4. AutoCompleteTextView
这个我们在开发中用的应该也挺多,很多时候用户在输入的时候,我们需要给用户更好的体验,什么刚好的体验呢?当用户输入一半的时候,我们能提示用户可能的输入就好了,那么此时这个控件就派上用场了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package cn.bridgeli.demo3; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import com.example.demo3.R; public class MainActivity extends ActionBarActivity { private AutoCompleteTextView autoCompleteTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); List<String> list = new ArrayList<String>(); list.add( "test1" ); list.add( "test2" ); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.textview, list); autoCompleteTextView.setAdapter(adapter); } } |
需要说明的是,本例中的提示用的是List,其实大家也可以换成数组之类的,不过个人认为一般提示的数据应该是从数据库中查询出来的,所以应该以List居多,例如当用户输入 a 时,提示所以以 a 打头的所有用户,对应的布局文件也有两个,分别是主布局文件和提示用户时出现的下拉框的布局文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo.MainActivity" > <AutoCompleteTextView android:id= "@+id/autoCompleteTextView" android:layout_width= "match_parent" android:layout_height= "match_parent" /> </LinearLayout> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo.MainActivity" > <TextView android:id= "@+id/textview" android:layout_width= "match_parent" android:layout_height= "match_parent" /> </LinearLayout> |
5. DatePickerDialog
这个用途其实也很多,例如当用户注册注册时填写注册日期之类的,那么这个控件就派上用了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package cn.bridgeli.demo; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; public class Dateactivity extends Activity { private Button button = null; private static final int DATE_ID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { showDialog(DATE_ID); } }); } @Override @Deprecated protected Dialog onCreateDialog(int id) { switch (id) { case DATE_ID: return new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int year, int month, int date ) { System.out.println(); } }, 2015, 0, 25); } return null; } } |
对应的肯定也有一个布局文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "cn.bridgeli.demo.MainActivity" > <Button android:id= "@+id/button" android:layout_width= "match_parent" android:layout_height= "match_parent" android:text= "Test" /> </LinearLayout> |
其实除了这些还有很多很多我们很常用的一些控件,例如:TimePickerDialog,其实和DatePickerDialog很类似,只不过一个是时间一个是日期仅此而已,有赖于我们工作中自己去学习积累,至于他们的用法其实也都不难,当我们用到一个控件时,如果我们不知道怎么用,先去查资料,然后根据资料写一个小例子测试一下,那个时候一个控件怎么用我们就会一目了然,所以学习的时候重要的是:自学的能力,只要你大胆的写,不要怕犯错就对了。
PS:第一次写Android的东西,既是初学还是自学,也没做过项目,所以不免有错误的地方和有点抓不住重点,还请大家能留言交流,谢谢
作 者: BridgeLi,https://www.bridgeli.cn
原文链接:http://www.bridgeli.cn/archives/141
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。
近期评论