Android开发教程ShowDialog工具类
Android开发教程ShowDialog工具类
已在模拟器测试运行,没发现bug。
通过本代码的演示,可以将UI相关(或其它方面)的常用操作封装成工具类,使代码复用程度更高,模块化更好,代码结构也更加清晰。
工具类UIHelper代码如下:
- package com.show.act;
- import Android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class UiHelper {
- /**
- * 提问框的 Listener
- *
- * @author Lei
- */
- // 因为本类不是activity所以通过继承接口的方法获取到点击的事件
- public interface OnClickYesListener {
- abstract void onClickYes();
- }
- /**
- * 提问框的 Listener
- *
- */
- public interface OnClickNoListener {
- abstract void onClickNo();
- }
- public static void showQuestionDialog(Context context, String title,
- String text, final OnClickYesListener listenerYes,
- final OnClickNoListener listenerNo) {
- Builder builder = new AlertDialog.Builder(context);
- if (!isBlank(text)) {
- // 此方法为dialog写布局
- final TextView textView = new TextView(context);
- textView.setText(text);
- LinearLayout layout = new LinearLayout(context);
- layout.setPadding(10, 0, 10, 0);
- layout.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT));
- builder.setView(layout);
- }
- // 设置title
- builder.setTitle(title);
- // 设置确定按钮,固定用法声明一个按钮用这个setPositiveButton
- builder.setPositiveButton(context.getString(R.string.yes),
- new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // 如果确定被电击
- if (listenerYes != null) {
- listenerYes.onClickYes();
- }
- }
- });
- // 设置取消按钮,固定用法声明第二个按钮要用setNegativeButton
- builder.setNegativeButton(context.getString(R.string.no),
- new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // 如果取消被点击
- if (listenerNo != null) {
- listenerNo.onClickNo();
- }
- }
- });
- // 控制这个dialog可不可以按返回键,true为可以,false为不可以
- builder.setCancelable(false);
- // 显示dialog
- builder.create().show();
- }
- /**
- * 处理字符的方法
- *
- * @param str
- * @return
- */
- public static boolean isBlank(String str) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return true;
- }
- for (int i = 0; i < strLen; i++) {
- if ((Character.isWhitespace(str.charAt(i)) == false)) {
- return false;
- }
- }
- return true;
- }
- }
使用示例如下:
- package com.show.act;
- import com.show.act.UiHelper.OnClickNoListener;
- import com.show.act.UiHelper.OnClickYesListener;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ShowDialogActivity extends Activity {
- //声明Button
- private Button showDialogButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init(){
- showDialogButton = (Button)findViewById(R.id.showDialog01);
- showDialogButton.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- //调用工具类中的dialog
- //需要传三个值到showQuestionDialog("当前界面","标题","提示内容",new 确定,new 取消 );
- UiHelper.showQuestionDialog(ShowDialogActivity.this, "提示", "是否确定", new OnClickYesListener() {
- public void onClickYes() {
- //点击确定干什么
- }
- }, new OnClickNoListener() {
- public void onClickNo() {
- //点击取消干什么
- }
- });
- }
- });
- }
- }
评论暂时关闭