自定义Dialog
自定义Dialog的创建
- 定义控件,
- onCreate()加载视图view
- 设置控件的监听事件
- 设置一个内部接口,定义抽象监听方法
- 构造方法回调listener
ChooseDialog.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ChooseDialog extends Dialog implements OnClickListener{
private Button btn_take_photo;
private Button btn_pick_photo;
private Button btn_pick_cancel;
private ChooserDialogListener listener;
public interface ChooserDialogListener{
public void onChoose(View view);
}
public ChooseDialog(Context context,int theme,ChooserDialogListener listener) {
super(context,theme);
this.listener = listener;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imgchooser_dialog);
initViews();
}
private void initViews(){
btn_take_photo = (Button)findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button)findViewById(R.id.btn_pick_photo);
btn_pick_cancel = (Button)findViewById(R.id.btn_pick_cancel);
btn_take_photo.setOnClickListener(this);
btn_pick_photo.setOnClickListener(this);
btn_pick_cancel.setOnClickListener(this);
}
public void onClick(View v) {
listener.onChoose(v);
this.dismiss();
}
}
Dialog调用
- 直接使用,初始化之后,dialog.show();
- DialogFragment中onCreateDialog(Bundle savedInstanceState)返回
源代码:DialogFragment.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39public Dialog onCreateDialog(Bundle savedInstanceState)
{
ChooseDialog dialog = new ChooseDialog(getActivity(),R.style.ActionTheme,
new ChooserDialogListener() {
public void onChoose(View view) {
switch(view.getId()){
case R.id.btn_take_photo:
((MainActivity)getActivity()).takeCamera();
break;
case R.id.btn_pick_photo:
((MainActivity)getActivity()).takePhoto();
break;
case R.id.btn_pick_cancel:
Log.i("Target", "取消");
break;
}
//TODO 分析图片
}
});
dialog.setCanceledOnTouchOutside(true);
Window window = dialog.getWindow();
//dialog全屏
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
//dialog显示在底层
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.mystyle);
//直接调用的话
//dialog.show();
return dialog;
}
AlertDialog简单示例
介绍:alert上有两个Numberpicker,一个Numberpicker为整数,一个Numberpicker表示小数点后一位。
通过AlertDialog.Builder去直接生成一个builder.create()生成一个Dialog,可以自己参看android源码
1 | public class MyDialogFragment extends DialogFragment implements OnValueChangeListener |
和Dialog一样 如果需要传值,它的调用处,去实现接口LoginInputListener,就可以先显示出来。
android传值,之前有写过一篇!传值其实都是类似,内部类,内部接口,似有变量设置getter,setter