<p> 先贴代码,马克留用,免得以后自己还要找....</p><p> </p><p>dialog_xml:</p>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/l_signupdialog_bg" android:orientation="vertical"> <TextView android:id="@+id/tv_EditDialogTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="15dip" android:gravity="center" android:paddingLeft="30dip" android:paddingRight="25dip" android:text="咨询" android:textColor="#1F1F1F" android:textSize="17sp" /> <EditText android:id="@+id/et_EditDialogText" android:layout_width="match_parent" android:layout_height="100dip" android:layout_below="@id/tv_EditDialogTitle" android:layout_marginLeft="25dip" android:layout_marginRight="25dip" android:layout_marginTop="5dip" android:background="#EFE0B9" android:ellipsize="end" android:gravity="left" android:hint="请填写咨询内容" android:maxLines="4" android:padding="5dip" android:textColor="#AA9053" android:textColorHint="#AA9053" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/et_EditDialogText" android:layout_marginRight="25dip" android:layout_marginTop="5dip" android:gravity="right" android:orientation="horizontal"> <TextView android:id="@+id/tv_SignUpDialogSubmitBt" android:layout_width="105dip" android:layout_height="35dip" android:background="@drawable/l_round_gray_bg" android:gravity="center" android:text="提交" android:textColor="#fff" /> </LinearLayout> </RelativeLayout>
java:
package com.example.yangletang.custom_commonent.others; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.text.Editable; import android.util.Log; import android.view.Display; import android.view.LayoutInflater; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.RelativeLayout; import com.example.yangletang.R; /** * 带有输入法的dialog * * @author LHT */ public class L_EditDialog extends Dialog { private Context context; private RelativeLayout layout; private Dialog dialog; private EditText et_EditDialogText; public L_EditDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.context = context; InitView(); } public L_EditDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); // TODO Auto-generated constructor stub } public L_EditDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub } private void InitView() { LayoutInflater inflaterDl = LayoutInflater.from(context); layout = (RelativeLayout) inflaterDl.inflate(R.layout.l_editdialog, null); et_EditDialogText = (EditText) layout.findViewById(R.id.et_EditDialogText); // dialog = new AlertDialog.Builder(context).create(); dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(layout); } public void showDialog() { dialog.show(); dialog.getWindow().setContentView(layout); Window win = dialog.getWindow(); win.getDecorView().setPadding(15, 0, 15, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; win.setAttributes(lp); } public void DissDialog() { dialog.dismiss(); } }
Window win = dialog.getWindow(); win.getDecorView().setPadding(15, 0, 15, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; win.setAttributes(lp);
马克一个drawable,用于做圆角dialog:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 填充颜色 <solid android:color="#fff"></solid> <!--<!– 线的宽度,颜色灰色 –>--> <!--<stroke--> <!--android:width="0.1dp"--> <!--android:color="#fff"></stroke>--> <!-- 矩形的圆角半径 --> <corners android:radius="3dp" /> </shape>
关于在
dialog = new AlertDialog.Builder(context).create();不能自动弹出输入法的解决办法:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);