在实际的开发当中,我们APP项目中的各种提示框都是统一的。为了避免重复的代码和样式的不同,所以我们需要定义一个统一样式的提示框!代码示例如下:
package com.wly.home.widget;
import com.wanbu.dascom.R;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
/**
* 统一样式的Dialog
* @author wangly
*
*/
public class CommonDialog extends Dialog implements android.view.View.OnClickListener {
private TextView tv_title;
private TextView tv_info;
private EditText et_inputInfo;
private TextView tv_number;
private TextView tv_cancle;
private TextView tv_ok;
private CallBackListener listener;
public void setListener(CallBackListener listener) {
this.listener = listener;
}
public CommonDialog(Context context) {
super(context,R.style.commonDialog_style);
intView();
}
private void intView() {
setContentView(R.layout.common_layout_dialog);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_info = (TextView) findViewById(R.id.tv_info);
et_inputInfo = (EditText) findViewById(R.id.et_inputInfo);
et_inputInfo.addTextChangedListener(watcher);
tv_number = (TextView) findViewById(R.id.tv_number);
tv_cancle = (TextView) findViewById(R.id.tv_cancle);
tv_ok = (TextView) findViewById(R.id.tv_ok);
tv_cancle.setOnClickListener(this);
tv_ok.setOnClickListener(this);
}
/**
* 设置标题
* @param text
* @param flag 显示/隐藏
*/
public void setTiltle(String text,int flag){
tv_title.setText(text);
tv_title.setVisibility(flag);
}
/**
* 设置Dialog描述信息
* @param text
* @param flag 显示/隐藏
*/
public void setInfo(String text,int flag){
tv_info.setText(text);
tv_info.setVisibility(flag);
}
/**
* 设置 "取消" 按钮文字
* @param text
*/
public void setCancleText(String text){
tv_cancle.setText(text);
}
/**
* 设置 "确定" 按钮文字
* @param text
*/
public void setConfirmText(String text){
tv_ok.setText(text);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_cancle:
if (listener!=null) {
listener.cancleListener();
}
break;
case R.id.tv_ok:
if (listener!=null) {
listener.confirmListener(et_inputInfo.getText().toString().trim());
}
break;
default:
break;
}
}
/**
* EditText 字符控制
*/
int num = 100;//限制的最大字数
TextWatcher watcher =new TextWatcher(){
private CharSequence temp;
private int selectionStart;
private int selectionEnd;
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
temp = s;
}
@Override
public void afterTextChanged(Editable s) {
int number = num - s.length();
tv_number.setText(s.length()+"/100");
selectionStart = et_inputInfo.getSelectionStart();
selectionEnd = et_inputInfo.getSelectionEnd();
if (temp.length() > num) {
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionEnd;
et_inputInfo.setText(s);
et_inputInfo.setSelection(tempSelection);//设置光标在最后
}
}
};
public interface CallBackListener{
/**
* 取消
*/
void cancleListener();
/**
* 确定
* 获取EditText 输入的内容
*/
void confirmListener(String text);
}
}
<style name="commonDialog_style" parent="@android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/update_dialog_shape</item>
</style>