`

Android AlertDialog包含EditText,软键盘不能弹出的解决方法

 
阅读更多
AlertDialog包含EditText,软键盘不能弹出的解决方法


[size=large]public static void editContentDialog(final Context context) {
		final AlertDialog dialog = new AlertDialog.Builder(context).create();
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.define_dialog_2, null);
		dialog.setView(layout);
		dialog.show();
		Window window = dialog.getWindow();
		// *** 主要就是在这里实现这种效果的.
		window.setContentView(R.layout.define_dialog_2);
		
		RelativeLayout rlCancel = (RelativeLayout) window.findViewById(R.id.rl_left);
		RelativeLayout rlBoundPhone = (RelativeLayout) window
				.findViewById(R.id.rl_right);
		TextView tvTitle = (TextView) window.findViewById(R.id.tv_dialog_title);
		final EditText etContent = (EditText) window
				.findViewById(R.id.et_content);
		TextView tvLeft = (TextView) window.findViewById(R.id.tv_left);
		TextView tvRight = (TextView) window.findViewById(R.id.tv_right);

		tvTitle.setText("新建列表");
		tvTitle.setVisibility(View.VISIBLE);
		etContent.setTextSize(16.0f);
		tvLeft.setText("取消");
		tvRight.setText("保存");
		rlCancel.setOnClickListener(new OnClickListener() {

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

		rlBoundPhone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失  
	}[/size]


define_dialog_2.xml
[size=large]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:background="@drawable/corner_box_white_2" >
        
        <TextView
            android:id="@+id/tv_dialog_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textColor="@color/black"
            android:text="手机绑定"
            android:visibility="visible"
            android:textSize="20sp" />

        <!-- <TextView
            android:id="@+id/tv_dialog_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="center"
            android:textColor="@color/black"
            android:layout_below="@id/tv_dialog_title"
            android:minHeight="40dp"
            android:text="法杰拉尔家乐福吉安网络服务了解放军阿拉维"
            android:textSize="20sp" /> -->
            <EditText 
                android:id="@+id/et_content"
                android:layout_width="match_parent"
            	android:layout_height="35dp"
            	android:layout_below="@id/tv_dialog_title"
            	android:layout_marginTop="15dp"
            	android:layout_marginLeft="10dp"
            	android:layout_marginRight="10dp"
            	android:singleLine="true"
            	android:ellipsize="end"
            	android:textColor="@color/black"
            	android:focusable="true"
            	android:focusableInTouchMode="true"
            	android:hint="请输入新列表名称"
            	android:gravity="center"
            	android:text="猎人"
            	android:background="@drawable/corner_box_white_90"
                />

        <View
            android:id="@+id/view_line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/et_content"
            android:layout_marginTop="15dp"
            android:background="@color/gray" />

        <View
            android:id="@+id/view_shuxian"
            android:layout_width="1dp"
            android:layout_height="45dp"
            android:layout_below="@id/view_line"
            android:layout_centerHorizontal="true" />

        <RelativeLayout
            android:id="@+id/rl_left"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_below="@id/view_line"
            android:layout_toLeftOf="@id/view_shuxian"
            android:background="@drawable/unicorn_left_bottom" >

            <TextView
                android:id="@+id/tv_left"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_right"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_below="@id/view_line"
            android:layout_toRightOf="@id/view_shuxian"
            android:background="@drawable/unicorn_right_bottom" >

            <TextView
                android:id="@+id/tv_right"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="20sp" />
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>
[/size]


  • 大小: 166.6 KB
分享到:
评论

相关推荐

    Android Dialog与软键盘的正确打开方式

    在Dialog中,当用户聚焦到EditText时,软键盘的弹出可能会导致Dialog布局的挤压,影响用户体验。为解决这个问题,有以下几种策略: 1. 设置Activity的`windowSoftInputMode`属性为`adjustResize`或`adjustPan`。`...

    禁止软键盘弹出,自定义键盘

    在Android中,当一个EditText组件获得焦点时,系统会默认弹出软键盘供用户输入。如果我们希望禁用这一行为,可以在布局文件中对EditText进行如下设置: ```xml &lt;EditText android:layout_width="wrap_content" ...

    SoftInputAlertDialog

    在Android开发中,"SoftInputAlertDialog"是一个常见的话题,它涉及到如何在对话框(Dialog)中集成EditText,并确保当软键盘弹出时,不会遮挡Dialog的内容。这通常发生在用户需要在Dialog中输入文本时,如登录、...

    Android中自定义的dialog中的EditText无法弹出输入法解决方案

     在show()方法调用之前,用dialog.setView(new EditText(context))添加一个空的EditText,由于是自定义的AlertDialog,有我们指定的布局,所以设置这个不会影响我们的功能,这样就可以弹出输入法了…… 2.可以弹出...

    Android自定义对话框

    本示例——"Android自定义对话框",主要探讨了如何创建一个自定义的Dialog,并且解决了在自定义View中包含EditText时无法弹出软键盘的常见问题。 首先,我们从`AlertDialog`开始。`AlertDialog`是Android系统提供的...

    安卓edittext范例

    在弹出式对话框(`AlertDialog`)中,可以使用 `AlertDialog.Builder.setView()` 添加 `EditText`,以便在对话框中接收用户输入。 8. **安全处理** 对于敏感信息(如密码),记得使用 `android:inputType="text...

    android密码对话框

    这里我们可以通过`InputMethodManager`来控制软键盘的显示和隐藏。 ```java LayoutInflater inflater = LayoutInflater.from(context); View dialogView = inflater.inflate(R.layout.dialog_password, null); ...

    仿支付宝支付弹框效果

    在Android应用开发中,创建一个仿支付宝支付弹框效果是一个常见的需求,这通常涉及到自定义View和Dialog的使用。本文将深入探讨如何实现这样一个功能,包括理解Dialog的基础知识、自定义View的步骤以及如何结合...

    Android开发入门书籍

    - 软键盘与硬键盘的支持 - 输入法事件处理 **2.6 使用选择控件(Chapter 12: Using Selection Widgets)** - **知识点:** - CheckBox与RadioButton - Switch与ToggleButton - Spinner与Adapter **2.7 列表...

    PopupWindow入门

    - 如果在PopupWindow中包含EditText,需确保在PopupWindow关闭时取消输入焦点,防止软键盘不自动隐藏。 通过上述介绍,你应该对Android中的PopupWindow有了基本的理解。在实际开发中,可以根据具体需求灵活运用...

    用户登陆对话框效果

    - 键盘自动弹出:当对话框显示时,可以自动聚焦到第一个EditText,使软键盘自动弹出。 - 记住密码功能:用户可以选择是否记住密码,以便下次自动填充。 - 遗忘密码链接:提供重置密码的功能链接,方便忘记密码的用户...

Global site tag (gtag.js) - Google Analytics