Android 完全自定义对话框的实现(标题栏+EditText+双按钮)

来源:互联网 发布:软件测试工作感想 编辑:程序博客网 时间:2024/05/21 10:00

纠结了我一下午,为了能使用我比较钟意的自定义对话框,我可谓绞尽脑汁,这里写下来 以表忠心。




这是我开始从网上看到的别人写的自定义框。博文地址在这:点击

我的目的不仅仅是提示框,我想将其改成可以在中间输入数据,然后按下确定我还可以获取其中的数据来用的对话框。

然后 我根据上面那篇博文进行修改,将其

dialog_normal_layout.xml

文件改成了如下形式:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:clickable="true"    android:orientation="vertical"    android:padding="20.0dip" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:background="@drawable/bg_bombbox"        android:orientation="vertical" >        <TextView            android:id="@+id/title"            style="@style/text_18_ffffff"            android:layout_width="fill_parent"            android:layout_height="40.0dip"            android:gravity="center"            android:text="@string/title_alert"            android:visibility="visible" />        <EditText            android:id="@+id/message"            style="@style/text_16_666666"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="http://"            android:gravity="left|center"            android:lineSpacingMultiplier="1.5"            android:minHeight="120.0dip"            android:paddingBottom="15.0dip"            android:paddingLeft="20.0dip"            android:paddingRight="20.0dip"            android:paddingTop="15.0dip" />        <View            android:layout_width="fill_parent"            android:layout_height="1.0px"            android:background="#ffd0d0d0" />        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="60.0dip"            android:layout_gravity="bottom"            android:background="@drawable/dialog_bottom_bg"            android:gravity="center"            android:orientation="horizontal" >            <Button                android:id="@+id/positiveButton"                style="@style/text_15_ffffff_sdw"                android:layout_width="114.0dip"                android:layout_height="40.0dip"                android:background="@drawable/btn_ok_selector"                android:gravity="center"                android:text="@string/ok" />            <Button                android:id="@+id/negativeButton"                style="@style/text_15_666666_sdw"                android:layout_width="114.0dip"                android:layout_height="40.0dip"                android:layout_marginLeft="20.0dip"                android:background="@drawable/btn_cancel_selector"                android:gravity="center"                android:text="@string/cancel" />        </LinearLayout>    </LinearLayout></FrameLayout>


对,我只是将TextView改成了EditText。 其它都没变。
然后在引用的地方,我改成了这样:



然后我一直就是拿不到EditText的数据, 到之后我才知道才看到, 那句dialog.dismiss(); 
 这里肯定是有问题的,但是我在发现之前是不知道的,但我用另一种方法实现了它。

另一种方法,参考的是这篇博文:点击

public class CustomDialog extends Dialog {Context  context;public CustomDialog(Context context) {super(context);this.context = context;}public CustomDialog(Context context, int theme) {super(context, theme);this.context = context;}@Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        this.setContentView(R.layout.dialog_normal_layout);    }}

这是自定义的dialog类,
 <style name="MyDialog" parent="@android:Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowNoTitle">true</item>         <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowContentOverlay">@null</item>    </style>
这是我定义的一个主题,主要是windowBackground那一项,必须设置成透明。

透明:
<color name="transparent">#00000000</color>

然后接下来就是如何使用了:
在要用到的地方:
 final CustomDialog dialog = new CustomDialog(getActivity(),R.style.MyDialog);dialog.show();Button confirm = (Button) dialog.findViewById(R.id.positiveButton);Button cancel = (Button) dialog.findViewById(R.id.negativeButton);final EditText edt = (EditText) dialog.findViewById(R.id.message);edt.setText(rtsp);confirm.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubdialog.dismiss();//pay attention}});cancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubdialog.dismiss();//pay attention}});

这样在onClick里是可以拿到edt的输入的数据的。

到此全部完成了。
上一个最终版本的图:


注意,这里的输入框是我点击才出现的,下面的Toast是在OnClick里输出的实时数据。

这个界面是在fragment里的,所以获取activity都是getactivity

0 0
原创粉丝点击