自定义AlertDialog对话框,包括解决对话框中edittext不能输入的问题

1、自定义代码实现:

private void addAndEditBookmark(final String typeStr, String title) {
// 1. 布局文件转换为View对象
LayoutInflater inflater = LayoutInflater.from(context);
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.bookmark_add, null);


// 2. 新建对话框对象
dialog = new AlertDialog.Builder(context).create();
dialog.setCancelable(false);
dialog.show();
Window window = dialog.getWindow();
window.setContentView(layout);
window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// 就是这个属性导致不能获取焦点,默认的是FLAG_NOT_FOCUSABLE,故名思义不能获取输入焦点
window.getAttributes().flags = window.getAttributes().FLAG_DIM_BEHIND;
window.setAttributes(window.getAttributes());
window.setLayout(screenWidth / 3, LayoutParams.WRAP_CONTENT);


// 3.标题内容
TextView dialog_title = (TextView) layout
.findViewById(R.id.title_bar_name);
dialog_title.setText(title);


// 4. 消息内容
dialog_msg = (EditText) layout.findViewById(R.id.input_add_string);
if ("add".equals(typeStr)) {
dialog_msg.setHint("请输入添加的标签名称");
} else if ("edit".equals(typeStr)) {
String str = (String) list.get(selectedIndex).get("markName");
dialog_msg.setHint(str);
}


// 5. 确定按钮
Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
btnOK.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
String str = dialog_msg.getText().toString();
if (null == str || "".equals(str) || "null".equals(str)) {
Toast.makeText(context, "标签名称不能为空", Toast.LENGTH_SHORT)
.show();
return;
} else {
addOrEditMark(typeStr, str);
}
dialog.dismiss();
}
});


// 6. 取消按钮
Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);
btnCancel.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}


2、对应的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center"
    android:padding="8dp" >


    <RelativeLayout
        android:id="@+id/layout_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_background"
        android:gravity="center_vertical" >


        <TextView
            android:id="@+id/title_bar_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:ellipsize="end"
            android:gravity="center"
            android:singleLine="true"
            android:text="添加书签"
            android:textColor="@color/custom"
            android:textSize="18sp"
            tools:ignore="HardcodedText" />
    </RelativeLayout>


    <EditText
        android:id="@+id/input_add_string"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout_title"
        android:layout_marginTop="10dp"
        android:background="@drawable/edittext_bg"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:textSize="18sp"
        tools:ignore="TextFields" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/input_add_string"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_weight="1"
            android:background="@drawable/btn_background"
            android:text="确定"
            android:textColor="@color/white"
            tools:ignore="HardcodedText" />


        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@drawable/btn_background"
            android:text="取消"
            android:textColor="@color/white"
            tools:ignore="HardcodedText" />
    </LinearLayout>


</RelativeLayout>



猜你喜欢

转载自blog.csdn.net/leihuanhuan123/article/details/52189909
今日推荐