前段时间,做了一个android涂鸦板项目(http://mm.10086.cn/1007/300001153536.html?fw=411130),在项目中,用到了一些自定义的对话框,如图所示。
在参考了android提供的ApiDemos程序以后,这里对如何实现自己定义的对话框进行一下总结。
在android的ApiDemos中的com.example.android.apis.graphics包下,有一个ColorPickerDialog类,是经典的自定义对话框的例子,我们在去除一些代码,剩下的主框架代码如下(代码中的注释详细注明每个类和方法的用途):
public class ColorPickerDialog extends Dialog {
/**
* 监听接口,通过此接口,可以把自定义Dialog需要向外界传递的信息,传递出去
*/
public interface OnColorChangedListener {
void colorChanged(int color);
}
/**
* 需要在对话框(ColorPickerDialog)上呈现的视图,我们自定义对话框的工作,主要就是构造出一个这样的视图
*/
private static class ColorPickerView extends View {
private OnColorChangedListener mListener;
/**
* ColorPickerView的构造函数,可以在这个构造函数里用android自带的控件,比如说LinearLayout、EditText和SeekBar等,把ColorPickerView构造出来。
* 也可以在onDraw(Canvas canvas)函数中,把ColorPickerView给绘制出来,ColorPickerView采用的就是这种方法。
*/
ColorPickerView(Context c, OnColorChangedListener l, int color) {
super(c);
}
/**
* 在这个函数里把ColorPickerView给绘制出来
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
}
/**
* 通过此方法来设置ColorPickerView的宽度和高度
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
}
/**
* 在这个函数里,调用监听接口OnColorChangedListener的方法,把此自定义Dialog需要向外界传递的信息设置进去
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
mListener.colorChanged(mCenterPaint.getColor());//这条语句调用了监听接口的方法,把信息设置了进去
}
return true;
}
}
/**
* 自定义对话框的构造方法
*/
public ColorPickerDialog(Context context,
OnColorChangedListener listener,
int initialColor) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnColorChangedListener l = new OnColorChangedListener() {
public void colorChanged(int color) {
mListener.colorChanged(color);
dismiss();
}
};
/**
* 只要通过ColorPickerDialog的setContentView方法,即可把我们辛苦绘制的ColorPickerView视图在对话框上呈现出来。
*/
setContentView(new ColorPickerView(getContext(), l, mInitialColor));
setTitle("Pick a Color");//这条语句设置对话框的标题
}
}
总的来说,创建自定义Dialog的关键只有两个步骤:
1.创建一个需要在自定义Dialog显示的自定义View,创建这个自定义View时,既可以在这个View的构造方法中用android自带的控件把自定义View构造出来;也可以在自定义View的@Override protected void onDraw(Canvas canvas)方法中,把自定义View绘制出来
2.在自定义Dialog的@Override protected void onCreate(Bundle savedInstanceState)方法中,通过setContentView(自定义View);方法,把我们的自定义View显示出来
在创建好自定义Dialog后,我们在别的类中,只要调用自定义Dialog的构造函数就可以把自定义Dialog显示出来。对于ColorPickerDialog这个类,调用语句如下:new ColorPickerDialog(getContext(), listener, mPaint.getColor()).show();
下面提供一个在自定义View的构造函数中把View构造出来的例子:
/**
* 文字对话框
*/
public class TextDialog extends Dialog implements SeekBar.OnSeekBarChangeListener{
private LinearLayout linearLayout;
private EditText etForText;
private SeekBar seekBar;
private TextView tvForSeekBar;
private Button btnOk;
private Button btnCancel;
private LinearLayout topChildLinearLayout;
private LinearLayout bottomChildLinearLayout;
private OnTextInputListener mListener;
/**
* 文字对话框标题
*/
private String title = "请输入文字与选择文字大小";
public interface OnTextInputListener {
void textInput(String text, int textSize);
}
public TextDialog(Context context, OnTextInputListener listener)
{
super(context);
mListener = listener;
linearLayout = new LinearLayout(getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
etForText = new EditText(context);
etForText.setMinLines(5);//设置最大行数
seekBar = new SeekBar(context);
seekBar.setLayoutParams(new LinearLayout.LayoutParams(200, LayoutParams.FILL_PARENT));
seekBar.setMax(100);
seekBar.setProgress(50);
seekBar.setOnSeekBarChangeListener(this);
tvForSeekBar = new TextView(context);
tvForSeekBar.setText("50");
btnOk = new Button(getContext());
btnOk.setText("确定");
btnCancel = new Button(getContext());
btnCancel.setText("取消");
linearLayout.addView(etForText);
topChildLinearLayout = new LinearLayout(getContext());
topChildLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
topChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
topChildLinearLayout.addView(seekBar);
topChildLinearLayout.addView(tvForSeekBar);
linearLayout.addView(topChildLinearLayout);
bottomChildLinearLayout = new LinearLayout(getContext());
bottomChildLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
bottomChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
bottomChildLinearLayout.setGravity(Gravity.CENTER);
bottomChildLinearLayout.addView(btnOk);
bottomChildLinearLayout.addView(btnCancel);
linearLayout.addView(bottomChildLinearLayout);
btnOk.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//验证text是否为空
String text = etForText.getText().toString()/*.replace("\n", "")*/;
if(text == null || text.trim().equals(""))
{
Toast.makeText(getContext(), "文字不能为空", Toast.LENGTH_SHORT).show();
return;
}
int textSizeInt = Integer.valueOf(tvForSeekBar.getText().toString());
mListener.textInput(text, textSizeInt);
dismissDialog();
}
});
btnCancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
dismissDialog();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(linearLayout);
setTitle(title);
}
public void dismissDialog()
{
this.dismiss();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
// TODO Auto-generated method stub
tvForSeekBar.setText(progress + "");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
}
此自定义Dialog的效果图如下:
全文结束,谢谢大家。
- 大小: 29.6 KB
- 大小: 8.8 KB
分享到:
相关推荐
整理了常用的dialog 包括:single Choice Dialog Multiple choice Dialog Input Dialog Progress Dialog
android 自定义 dialog android 自定义 dialog android 自定义 dialog android 自定义 dialog
android自定义全屏dialog,可以全屏显示图片。
Dialog 很经常用到的一个控件,但大多时候系统默认的样式是不满足于项目使用的,故自定义了一套合适的Dialog模板方便日常使用。
android自定义dialog
Android自定义dialog,需要的继续带走
自己整理的自定义控件方法,看了你就会做自定义Dialog和Toast了,没有多余代码,很简洁。背景、按钮都能自定义
android自定义Dialog下载文件 ,在自定义的Dialog上显示文件下载的速度和ProgressBar进度,共享给大家! 欢迎指点提议 Email:vipa1888@163.com QQ 840950105 Author: spring sky
android自定义dialog嵌套listview,有点击事件
Android自定义dialogDemo
很好android自定义dialog加载转圈等待,适合初学,透明+正常两种主题,屏蔽返回键(自己修改参数,主题也是)
android自定义Dialog的简单实现,具体实现可以查看博客:http://blog.csdn.net/qq_20785431/article/details/53396985
android利用布局文件自定义Dialog、Toast
一个可以直接使用的自定义AlertDialog,封装的很完整的一个Demo,可以灵活使用,里面提示图标还额外添加了动画效果,希望可以帮到大家。
android 自定义dialog 带有各种显示动画效果
项目中需要有个Dialog全选对话框,点击全选全部选中,取消全选全部取消。下午查了些资料,重写了一下Dialog对话框。把代码放出来。
通过简单的自定义Dialog来理解接口回调callback机制
Android自定义控件:可重用的自定义Dialog类
Dialog对话框之android 自定义对话框代码 设置对话框显示一个单选的List 用来设置对话框显示一系列的复选框 设置进度条风格,风格为圆形,旋转的