android AlertDialog中的Edittext自动获取焦点并弹出软键盘

在自定义的dialog中的Oncreate中 加

 this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ed.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);
                    }
                },200);

            }
        });

完整源码

package com.huikeyun.teacher.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.huikeyun.teacher.common.R;
import com.xw.repo.XEditText;


/**
 * 带titleAlertDialog
 */
public class DialogPrompt extends Dialog {
    public static DialogPrompt mInstance;
    private TextView bt_left, bt_right;
    private static Context mContext;
    private TextView tv_message;
    private XEditText ed;

    public DialogPrompt(Context mContext) {
        super(mContext);
    }

    private static final String TAG = "DialogAsk";

    public DialogPrompt(Context context, int theme) {
        super(context, theme);
    }

    public static DialogPrompt getIstance(Context context) {
        mContext = context;
        try {
            mInstance = new DialogPrompt(context, R.style.MyDialog);
            return mInstance;
        } catch (Exception e) {
            Log.e(TAG, "getIstance: " + e.toString());
            mInstance = null;
            return null;
        }
    }

    //    @Override
//    public void onCreate(Bundle savedInstanceState){
//        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//需要在设置内容之前定义
//        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//        setContentView(R.layout.dialog_setting_onetoggle);
//
//
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_prompt);
        initViews();
        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ed.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);
                    }
                },200);

            }
        });
    }

    /**
     * 确定按钮接口
     */
    public interface onNoOnclickListener {
        public void onNoClick();
    }

    /**
     * 取消按钮接口
     */
    public interface onYesOnclickListener {
        public void onYesOnclick(String trim);
    }


    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) {

        this.noOnclickListener = onNoOnclickListener;
    }

    public void setMessage(String text) {
        if (tv_message != null) {
            tv_message.setText(text);
        } else {
//            等待初始化以后再加上文字
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    tv_message.setText(text);
                }
            }, 100);
        }

    }

    /**
     * 设置确定按钮的显示内容和监听
     */
    public void setYesOnclickListener(onYesOnclickListener yesOnclickListener) {
        this.yesOnclickListener = yesOnclickListener;
    }


    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器


    void initViews() {
        bt_left = (TextView) findViewById(R.id.tv_dso_canel);
        bt_right = (TextView) findViewById(R.id.tv_dso_ok);
        tv_message = (TextView) findViewById(R.id.tv_dp_message);
        ed = (XEditText) findViewById(R.id.ed_ap);
        ed.requestFocus();

        AssetManager mgr = mContext.getAssets();//得到AssetManager  
        Typeface tf = Typeface.createFromAsset(mgr, "fonts/PingFang Medium.ttf");//根据路径得到Typeface  
        bt_right.setTypeface(tf);//设置字体  
        bt_left.setTypeface(tf);//设置字体  

        //设置确定按钮被点击后,向外界提供监听
        bt_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
        //设置取消按钮被点击后,向外界提供监听
        bt_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesOnclick(ed.getTextEx().toString().trim());
                }
            }
        });


    }
}

自定义对话框也可以写在代码的外面

private void confirmPhoneGurdPswd(final String guardPswd) {
 
        // 1.创建弹出式对话框  
        final AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(HomeActivity.this); // 系统默认Dialog没有输入框  
 
        // 获取自定义的布局  
        View alertDialogView = View.inflate(HomeActivity.this, R.layout.自定义布局, null);
 
        // 2.密码框-EditText。alertDialogView.findViewById(R.id.自定义布局中的文本框)  
        final EditText et_dialog_confirmphoneguardpswd = (EditText) alertDialogView.findViewById(R.id.et_dialog_confirmphoneguardpswd);
 
        // 确认按钮,确认验证密码  
        Button btn_dialog_resolve_confirmphoneguardpswd = (Button) alertDialogView.findViewById(R.id.btn_dialog_resolve_confirmphoneguardpswd);
        btn_dialog_resolve_confirmphoneguardpswd.setOnClickListener(new OnClickListener() {
            // 点击按钮处理  
            public void onClick(View v) {
                // 提取文本框中输入的文本密码  
            }
        });
        // 取消按钮,不验证密码  
        Button btn_dialog_cancel_confirmphoneguardpswd = (Button) alertDialogView.findViewById(R.id.btn_dialog_cancel_confirmphoneguardpswd);
        btn_dialog_cancel_confirmphoneguardpswd.setOnClickListener(new OnClickListener() {
            // 点击按钮处理  
            public void onClick(View v) {
                //  
            }
        });
 
        AlertDialog tempDialog = alertDialog.create();
        tempDialog.setView(alertDialogView, 0, 0, 0, 0);
 
        /** 3.自动弹出软键盘 **/
        tempDialog.setOnShowListener(new OnShowListener() {
            public void onShow(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(et_dialog_confirmphoneguardpswd, InputMethodManager.SHOW_IMPLICIT);
            }
        });
 
        tempDialog.show();
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值