少年游

欲买桂花同载酒,终不似,少年游。

0%

android之自定义Dialog

自定义Dialog

自定义Dialog的创建

  1. 定义控件,
  2. onCreate()加载视图view
  3. 设置控件的监听事件
  4. 设置一个内部接口,定义抽象监听方法
  5. 构造方法回调listener

ChooseDialog.java

1
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
45
import 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;
}

@Override
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);
}

@Override
public void onClick(View v) {
listener.onChoose(v);
this.dismiss();
}
}

Dialog调用

  1. 直接使用,初始化之后,dialog.show();
  2. DialogFragment中onCreateDialog(Bundle savedInstanceState)返回

源代码:DialogFragment.java

1
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
public Dialog onCreateDialog(Bundle savedInstanceState)
{

ChooseDialog dialog = new ChooseDialog(getActivity(),R.style.ActionTheme,
new ChooserDialogListener() {
@Override
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
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
45
46
47
48
49
50
51
52
public class MyDialogFragment extends DialogFragment implements OnValueChangeListener
{

private NumberPicker np_integer;
private NumberPicker np_decimal;

public interface LoginInputListener
{
void onLoginInputComplete(float result);
}

...

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog, null);
np_integer = (NumberPicker) view.findViewById(R.id.np_integer);
np_integer.setMinValue(0);
np_integer.setMaxValue(999);
np_integer.setOnValueChangedListener(this);
np_decimal = (NumberPicker) view.findViewById(R.id.np_decimal);
np_decimal.setMinValue(0);
np_decimal.setMaxValue(9);
np_decimal.setOnValueChangedListener(this);
builder.setView(view)
.setTitle("PLEASE INPUT AND SAVE")
// Add action buttons
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
Log.i("Target", getTargetFragment().getClass().getName());
LoginInputListener listener = (LoginInputListener) getTargetFragment();
float result= np_integer.getValue() + np_decimal.getValue()*1.0f/10;
listener.onLoginInputComplete(result);
}
}).setNegativeButton("Cancel", null);
return builder.create();
}

...

@Override
onValueChange //不写了

}

和Dialog一样 如果需要传值,它的调用处,去实现接口LoginInputListener,就可以先显示出来。
android传值,之前有写过一篇!传值其实都是类似,内部类,内部接口,似有变量设置getter,setter