纠结了我一下午,为了能使用我比较钟意的自定义对话框,我可谓绞尽脑汁,这里写下来 以表忠心。
这是我开始从网上看到的别人写的自定义框。博文地址在这:点击
我的目的不仅仅是提示框,我想将其改成可以在中间输入数据,然后按下确定我还可以获取其中的数据来用的对话框。
然后 我根据上面那篇博文进行修改,将其
<?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>
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); } }
<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() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dialog.dismiss();//pay attention } }); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dialog.dismiss();//pay attention } });