dialog简单记录

1.构造一个简单的dialog并使用。

public class Main3Activity extends AppCompatActivity {
    private AlertDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mDialog = new AlertDialog.Builder(this)
                .setMessage("简单消息框")
                .setPositiveButton("确定", null)
                .create();
        mDialog.show();
    }
}

2.给dialog中的Message的内容添加Scroll。
如果setMessage中的内容太长导致dialog显示不全,此时可以将内容设置成滚动或者设置dialog的宽度和高度。

AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("YOUR_TITLE")
                .setMessage("YOUR_MSG")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_info)
                .show();
        TextView textView = (TextView) dialog.findViewById(android.R.id.message);
        textView.setMaxLines(5);
        textView.setScroller(new Scroller(this));
        textView.setVerticalScrollBarEnabled(true);
        textView.setMovementMethod(new ScrollingMovementMethod());

3.设置dialog的宽度和高度。
这里给出setAttributes、setContentView以及setLayout的这3种使用方法。注意:设置dialog大小的代码必须放在dialog.show()之后,具体原因可参考这篇源码分析Dialog自定义大小无效坑

setAttributes

AlertDialog dialog = builder.create();
dialog.setView(view);
dialog.show();
WindowManager m = getWindowManager();  
Display d = m.getDefaultDisplay();  //为获取屏幕宽、高  
android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值  
p.height = (int) (d.getHeight() * 0.3);   //高度设置为屏幕的0.3
p.width = (int) (d.getWidth() * 0.5);    //宽度设置为屏幕的0.5 
dialog.getWindow().setAttributes(p);     //设置生效  

setContentView

Dialog dialog = new Dialog(this, R.style.Dialog);
dialog.show();
LayoutInflater inflater = LayoutInflater.from(this);
View viewDialog = inflater.inflate(R.layout.adapter_list, null);
Display display = this.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
ViewGroup.LayoutParams layoutParams = new  ViewGroup.LayoutParams(width, height);
dialog.setContentView(viewDialog, layoutParams);

setLayout

dialog.show();
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

有时候,是无法使用getWindowManager的,因此setLayout还是很有用处的。
4.给dialog添加view。
以下是一个在dialog中添加EditText,并且监听dialog的实例(应用在没有触摸屏只有按键的设备)。

public class Main3Activity extends AppCompatActivity {
    private EditText mEditText;
    private AlertDialog mDialog;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mEditText = new EditText(this);
        mDialog = new AlertDialog.Builder(this)
                .setView(mEditText)
                .setMessage("简单消息框")
                .setPositiveButton("确定", null)
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                if (mEditText.hasFocus()) {
                                    mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                                }
                                break;
                            case KeyEvent.KEYCODE_DPAD_DOWN:
                                if (mEditText.hasFocus()) {
                                    mEditText.clearFocus();
                                    mDialog.getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();
                                }
                                break;
                        }
                        return false;
                    }
                })
                .setCancelable(false)
                .create();
        mDialog.show();
    }
}

这里通过调用setView,给dialog添加了一个EditText。
不带触摸屏的设备EditText的焦点需要通过监听按键去另外处理,而dialog中通过setOnKeyListener监听按键。至于是否拦截事件,看具体需求,如果需要拦截就返回true,因为返回true就会消费事件,消费掉了就等于拦截掉了。
而由于不带触屏的设备没有删除功能,mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));就相当于软键盘的删除键,因此case KeyEvent.KEYCODE_BACK中添加该代码。
还有就是不带触屏的设备中EditText焦点处理问题。此时需通过监听KEYCODE_DPAD_DOWN去清除焦点(调用clearFocus),并且button需请求焦点(可通过AlertDialog 中的getButton(DialogInterface.BUTTON_POSITIVE)获取到button,然后调用requestFocus请求焦点)。
setCancelable(false);
是设置点击屏幕或物理返回键,dialog不消失。

如果在不带触屏的设备中,如果setMessage的内容太长,并且就算设置了宽度和高度也无法解决显示不全的问题的话,就可以通过setView添加一个带Scrollview的layout文件去代替setMessage去显示。

public class Main3Activity extends AppCompatActivity {
    private AlertDialog mDialog;

    private ScrollView mScrollView;
    private EditText mEditText;
    private View mView;

    private boolean isScrollTop;
    private boolean isScrollBottom;

    private boolean isFocusInEditText;
    private boolean isFocusInScroll = true;
    private boolean isFocusInButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mView = getLayoutInflater().inflate(R.layout.layout_view, null);
        mEditText = (EditText) mView.findViewById(R.id.edit_text);
        mScrollView = (ScrollView) mView.findViewById(R.id.scroll_view);

        mDialog = new AlertDialog.Builder(this)
                .setView(mView)
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP && KeyEvent.ACTION_UP==event.getAction()){
                            if (isFocusInButton){
                                mDialog.getButton(DialogInterface.BUTTON_POSITIVE).clearFocus();
                                mEditText.requestFocus();
                                isFocusInEditText = true;
                                isFocusInButton = false;
                            }else if (mScrollView.getScrollY() <= 0  && !isScrollTop) {
                                isFocusInScroll = true;
                                isScrollBottom = false;
                                isScrollTop = true;
                            }else if (isFocusInEditText){
                                mEditText.clearFocus();
                                mScrollView.requestFocus();
                                isFocusInEditText = false;
                                isFocusInScroll = true;
                            }
                        }
                        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN && KeyEvent.ACTION_UP==event.getAction()){
                            if (mScrollView.getChildAt(0).getMeasuredHeight() <=
                                    mScrollView.getHeight() + mScrollView.getScrollY() && !isScrollBottom) {
                                isScrollBottom = true;
                                isFocusInScroll = true;
                                isScrollTop = false;
                            }else if (isFocusInScroll && isScrollBottom){
                                mScrollView.clearFocus();
                                mEditText.requestFocus();
                                isFocusInEditText = true;
                                isFocusInScroll = false;
                            }else if (isFocusInEditText){
                                mEditText.clearFocus();
                                mDialog.getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();
                                isFocusInEditText = false;
                                isFocusInButton = true;
                            }
                        }
                        switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                if (mEditText.hasFocus()) {
                                    mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                                }
                                break;
                        }
                        return false;
                    }
                })
                .setPositiveButton("确定", null)
                .setCancelable(false)
                .create();
        mDialog.show();

    }
}

由于上下按键部分时候会响应2次,所以监听中需加上
event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP && KeyEvent.ACTION_UP==event.getAction()event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN && KeyEvent.ACTION_UP==event.getAction()
layout_view.xml如下所示。


    
        
    
    

参考链接:
Adding a vertical scrollbar to an AlertDialog in Android?

Android Dialog AlertDialog 设置高度和宽度

Android Dialog全屏显示

Android:修改AlertDialog的背景并动态控制AlertDialog的最大高度

源码分析Dialog自定义大小无效坑

按钮控制EditText删除单个字符

Android 拦截返回键事件

对话框中dialog.setCancelable与setCanceledOnTouchOutside的区别

android AlertDialog setView rules

Android-使用ScrollView设置滚动界面

android监听软键盘回车键并且解决默认点击两次的问题

android scrollview 不滑动

你可能感兴趣的:(dialog简单记录)