在Android的界面开发,对于一些控件中的使用会使用到特殊的方法和机制。在界面开发部分的学习中,这些方法需要特别注意。现将其中的部分总结出来,便于查询。
1. 自定义对话框
Android中对话框控件的定义和使用有些与众不同,需要利用AlertDialog.Builder类设置对话框的各属性,然后调用其creat()方法得到Dialog对象。调用Dialog对象的show()方法显示对话框。
自定义对话框的原理是利用Builder对象的setView()方法,将一个View对象传入其中,这个View对象来自于自定义的作为对话框中布局的布局文件。自定义的View对象通过LayoutInflater对象获得。示例代码如下:
-
- LayoutInflater factory=LayoutInflater.from(three.this);
-
- final View dia=factory.inflate(R.layout.dialog, null);
-
- AlertDialog.Builder builder=new AlertDialog.Builder(three.this);
-
- builder.setTitle("登录提示");
- builder.setView(dia);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- three.this.displayToast("登录成功!");
- }
- });
- builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- three.this.finish();
-
- }
- });
-
- Dialog dialog=builder.create();
-
- dialog.show();
-
|
2. BaseAdapter类的使用
前面曾经使用过AraryAdapter、ListAdapter和SimpleCurAdapter,它的使用都很简单,就是获得适配器对象然后将数据源和布局参数作为构造方法的参数传递进去就行。以上三个类都是BaseAdapter的子类,自定义一个继承了BaseAdapter类的子类同样可以实现以上三个类的功能。具体代码如下:
- public class myAdapter extends BaseAdapter{
-
- private Context context;
-
- ArrayList<HashMap<String,String>> arrlist=new ArrayList<HashMap<String,String>>();
- HashMap<String, String> map1 = new HashMap<String, String>();
- HashMap<String, String> map2 = new HashMap<String, String>();
- HashMap<String, String> map3 = new HashMap<String, String>();
-
- public myAdapter(Context context){
- map1.put("name","张三");
- map1.put("id", "1");
- map2.put("name","李四");
- map2.put("id", "2");
- map3.put("name","王五");
- map3.put("id", "3");
- arrlist.add(map1);
- arrlist.add(map2);
- arrlist.add(map3);
- this.context=context;
- }
-
- @Override
- public int getCount() {
-
- return arrlist.size();
- }
-
- @Override
- public Object getItem(int position) {
-
- return position;
- }
-
- @Override
- public long getItemId(int position) {
-
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
-
- LayoutInflater layoutInf=LayoutInflater.from(context);
- View view=layoutInf.inflate(com.skip.R.layout.lt, null);
- HashMap<String,String> map=arrlist.get(position);
- TextView tx1=(TextView)view.findViewById(com.skip.R.id.widget28);
- tx1.setText(map.get("name"));
- TextView tx2=(TextView)view.findViewById(com.skip.R.id.widget29);
- tx2.setText(map.get("id"));
- return view;
- }
-
- }
|
在上面的类中,数据源是写死在类中的,可以是List,Cursor和数组,而在getView中是将数据绑定在控件上的操作,返回View对象。在子类中,重点就是实现getView()方法和数据源的定义,其他的方法直接按照方法功能返回所需数据即可。初始化该对象,并将上面的对象直接作为控件的适配器就可以直接使用。
上段代码等同于如下代码:
- ArrayList<HashMap<String,String>> arrlist;
- arrlist=new ArrayList<HashMap<String,String>>();
- HashMap<String, String> map1 = new HashMap<String, String>();
- HashMap<String, String> map2 = new HashMap<String, String>();
- HashMap<String, String> map3 = new HashMap<String, String>();
- map1.put("name","张三");
- map1.put("id", "1");
- map2.put("name","李四");
- map2.put("id", "2");
- map3.put("name","王五");
- map3.put("id", "3");
- arrlist.add(map1);
- arrlist.add(map2);
- arrlist.add(map3);
- SimpleAdapter sada=new SimpleAdapter(this,
- AdapyerAxarrlist,
- android.R.layout.simple_list_item_2,
- new String[]{"name","id"},
- new int[]{android.R.id.text2,android.R.id.text1});
- ListView ls = (ListView)findViewById(R.id.list);
- ls.setAdapter(sada);
|
BaseAdapter作为抽象的父类,其数据源可以是任何一种,数据源和布局都是=可以自定义。SimpleAdapte作为BaseAdapter的子类,具有特定功能,它的数据源特定并由外部传入,使用起来相对方便。
但是,BaseAdapter具有自己的优势,那就是可以将媒体文件作为数据源元素加载到适配器并更加详细的定义其布局,这是其子类无法做到的。代码如下:
- public class ImageAdapter extends BaseAdapter
- {
-
- private Context mContext;
-
- private Integer[] mImageIds =
- {
- R.drawable.img1,
- R.drawable.img2,
- R.drawable.img3,
- R.drawable.img4,
- R.drawable.img5,
- R.drawable.img6,
- R.drawable.img7,
- R.drawable.img8,
- };
-
-
- public ImageAdapter(Context c)
- {
- mContext = c;
- }
-
-
- public int getCount()
- {
- return mImageIds.length;
- }
-
-
- public Object getItem(int position)
- {
- return position;
- }
-
-
- public long getItemId(int position)
- {
- return position;
- }
-
- public View getView(int position, View convertView, ViewGroup parent)
- {
- ImageView p_w_picpathview = new ImageView(mContext);
-
-
- p_w_picpathview.setImageResource(mImageIds[position]);
-
- p_w_picpathview.setLayoutParams(new Gallery.LayoutParams(120, 120));
-
- p_w_picpathview.setScaleType(ImageView.ScaleType.FIT_CENTER);
- return p_w_picpathview;
- }
- }
|
使用BaseAdapter类对于那些需要详细定义布局的数据源具有很好的操作性,特别是对于图片这类属性较多的数据,一般在使用适配器时选择BaseAdapter。
3. ImageSwitcher的使用
ImageSwitcher可以实现图片的切换效果,它的使用并非很难,但是容易忽略ImageSwitch的对象需要利用setFactory()方法设置转换器每一页的View。在这个设置完成以后才可以添加数据源。使用代码如下:
- p_w_picpathswi=(ImageSwitcher)findViewById(R.id.imgswi);
- p_w_picpathswi.setFactory(new ViewSwitcher.ViewFactory() {
-
- @Override
- public View makeView() {
-
- return new ImageView(three.this);
- }
- });
-
- p_w_picpathswi.setImageResource(p_w_picpathlist[index]);
|