2017年开始了,重新开始自己的工作积累和记录,开年头一篇!
自定义对话框代码类:
package com.customcontrol;
import com.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class CustomDialog extends Dialog implements OnClickListener {
private Context _context;
/**
* 标题
*/
private String title = "提示";
/**
* 内容
*/
private String content = "";
/**
* 确定按钮
*/
private Button btnSave;
/**
* 关闭按钮
*/
private Button btnClose;
/**
* 标题
*/
private TextView txtTitle;
/**
* 提示内容
*/
private TextView txtContent;
/**
* 样式
*/
private int layout = R.layout.custom_dialog;
/**
* 视图
*/
private LayoutInflater inflater;
private View view;
/**
* 自定义监听事件
*/
private OnCustomDialogListener customDialogListener;
/**
* 设置标题
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* 设置提示内容
* @param content
*/
public void setContent(String content) {
this.content = content;
}
/**
* 设置样式
* @param theme
*/
public void setLayout(int layout) {
this.layout = layout;
}
/**
* 监听事件
* @param customDialogListener
*/
public void SetOnCustomDialogListener(OnCustomDialogListener customDialogListener)
{
this.customDialogListener = customDialogListener;
}
/**
* 事件接口
* @author Administrator
*
*/
public interface OnCustomDialogListener{
public void CustomDialogListener();
}
protected CustomDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
}
public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this._context = context;
}
public CustomDialog(Context context, String content) {
super(context);
// TODO Auto-generated constructor stub
this._context = context;
this.content = content;
}
public CustomDialog(Context context, String title, String content) {
super(context);
// TODO Auto-generated constructor stub
this._context = context;
this.title = title;
this.content = content;
}
public CustomDialog(Context context, int theme)
{
super(context, theme);
this._context = context;
}
public CustomDialog(Context context, int theme, String content)
{
super(context, theme);
this._context = context;
this.content = content;
}
public CustomDialog(Context context, int theme, String title, String content) {
super(context, theme);
// TODO Auto-generated constructor stub
this._context = context;
this.title = title;
this.content = content;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) _context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layout, null);
this.setContentView(view);
btnSave = (Button)view.findViewById(R.id.btnOk);
btnClose = (Button)view.findViewById(R.id.btnClose);
txtTitle = (TextView)view.findViewById(R.id.txtDialogTitle);
txtContent = (TextView)view.findViewById(R.id.txtDialogContent);
btnSave.setOnClickListener(this);
btnClose.setOnClickListener(this);
txtTitle.setText(title);
txtContent.setText(content);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnClose:
this.cancel();
break;
case R.id.btnOk:
customDialogListener.CustomDialogListener();
break;
}
}
}
对话框引用处
CustomDialog dialog = new CustomDialog(this, R.style.customdialog);
dialog.SetOnCustomDialogListener(new OnCustomDialogListener() {
@Override
public void CustomDialogListener() {
// TODO Auto-generated method stub
JSONObject jsonObj = new JSONObject();
try {
//点击确定执行自己的方法
} catch (JSONException e) {
e.printStackTrace();
}
}
});
dialog.setContent("确定执行吗?");
dialog.show();
style代码:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="customdialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/yellow</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
自定义了一个AlertDialog处理类,为了可以自定义样式,之前显示正常的app,在其他设备重试之后,发现不能显示弹框内容,不论内容是设置在初始化代码中还是引用的位置均无效,最终,在布局文件XMl中也不显示,于是猜想应该是文本存在,但是显示上的问题,大胆猜想,在自定义的dialog布局文件中将content的textView的文本颜色设置为黑色解决……
代码如下:
总结: AlertDialog默认背景颜色是白的,里面的文字也是白的,所以看上去就像空的一样!