【Android基础】(9)UI控件(三)

文章出处:http://blog.csdn.net/scarthr/article/details/42006611

今天我们来UI控件的最后一篇。


一 ImageSwitcher

ImageSwicher图片切换控件,xml:
    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ImageSwitcher>
使用方法如下:
package com.thr.testandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

	private ImageSwitcher switcher;
	private boolean showImg1 = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
		switcher.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				return new ImageView(MainActivity.this);
			}
		});
		switcher.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showImg1 = !showImg1;
			}
		});
		showCurrentImage();

	}

	private void showCurrentImage() {
		if (showImg1) {
			switcher.setImageResource(R.drawable.img1);
		} else {
			switcher.setImageResource(R.drawable.img2);
		}
	}
}
再使用ImageSwitcher的时候需要先setFactory,创建一个ViewFactory对象makeView方法返回一个ImageView对象即可,最后再点击ImageView事件中对两个图片进行切换。上面这样子的代码是没有什么特色的,ImageSwitcher是可以加入图片切换动画效果的,加入如下代码:
		switcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
这样就加入的淡入和淡出的动画效果。

二 Gallery

这个组件“画廊”是一个水平切换图片的控件。布局文件:
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />
Java文件:
		Gallery gallery = (Gallery) findViewById(R.id.gallery1);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1);
		for (int i = 0; i < 10; i++) {
			adapter.add("测试" + i);
		}
		gallery.setAdapter(adapter);
	}
这就实现了水平切换的控件效果。

三 EditText

EditText应该是我们非常非常常见的一个控件了,关于它的输入控制是很多的。xml文件:
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />
inputType的种类有很多,这里textPassword是使用密码做为输入控制。ems控制EditText长度超过10后不显示。
他的使用非常简单:
		final EditText et = (EditText) findViewById(R.id.editText1);
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(et.getText().toString())) {
					// 对输入内容做想做的事情。
				}
			}
		});


四 BaseAdapter

我们现在对BaseAdapter做一个深入的使用,布局文件是一个ListView和两个按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="增加一项" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="删除一项" />
    </LinearLayout>

</LinearLayout>
我们使用两个按钮来控制ListView的增加和删除
然后这里我们创建一个适配器ListAdapter继承BaseAdapter:
package com.thr.testandroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class ListAdapter<T> extends BaseAdapter {

	private List<T> list = new ArrayList<T>();
	private Context context;
	private int resId;

	public ListAdapter(Context context, int resId) {
		this.context = context;
		this.resId = resId;
	}

	public void add(T item) {
		list.add(item);
	}

	public void remove(int position) {
		list.remove(position);
	}

	public void removeLast() {
		list.remove(getCount() - 1);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public T getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(resId, null);
		}
		initView(position, convertView, parent);
		return convertView;
	}

	protected abstract void initView(int position, View convertView,
			ViewGroup parent);
}
我们自己写了一个initView方法来控制第一次初始化数据。最后Activity入下:
package com.thr.testandroid;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ListActivity implements OnClickListener {

	private ListAdapter<String> adapter;
	private Button btnAdd;
	private Button btnRemove;
	private int index = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		adapter = new ListAdapter<String>(this,
				android.R.layout.simple_list_item_1) {

			@Override
			protected void initView(int position, View convertView,
					ViewGroup parent) {
				((TextView) convertView).setText(getItem(position));
			}
		};
		setListAdapter(adapter);
		for (index = 0; index < 10; index++) {
			adapter.add("测试数据" + index);
		}
		btnAdd = (Button) findViewById(R.id.btn_add);
		btnRemove = (Button) findViewById(R.id.btn_remove);

		btnAdd.setOnClickListener(this);
		btnAdd.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			index++;
			adapter.add("测试" + index);
			break;
		case R.id.btn_remove:
			index++;
			adapter.removeLast();
			break;
		}
	}

}
我们测试下,发现在点击增加和删除后ListView并没有发生改变,这是为什么呢?难道没有加上或删掉吗?其实不是的,后台确实增加或者删除了,只是我们在界面上没有刷新。那么该怎么办呢?当ListView数据发生改变时,我们需要发出一条通知告诉ListView数据发生改变了,我们在ListAdapter的add和remove方法中加入这样子的一行代码
		notifyDataSetChanged();
然后再运行,增加删除看看,是不是都好了?

五 后退事件

当我们需要重写系统后退事件的时候,需要覆盖Activity的onBackPressed()方法,在里面写我们需要的操作。下来我们写一个监听2次短时间内点击退出键退出应用的例子:
	private long lastClickTime = 0;

	@Override
	public void onBackPressed() {
		if (lastClickTime <= 0) {
			Toast.makeText(this, "再次点击退出应用", Toast.LENGTH_SHORT).show();
			lastClickTime = System.currentTimeMillis();
		} else {
			if (System.currentTimeMillis() - lastClickTime < 1000) {
				finish();
			} else {
				Toast.makeText(this, "再次点击退出应用", Toast.LENGTH_SHORT).show();
				lastClickTime = System.currentTimeMillis();
			}
		}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值