Android自定义样式的对话框
jopen
10年前
首先看下效果图,确保是你想要的效果:
xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/pop_bg" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_marginTop="15dp" android:gravity="center" android:text="设置号码" android:textColor="#fffffb" android:textSize="20sp" android:visibility="visible" /> <!-- <LinearLayout android:id="@+id/contentView" android:layout_width="fill_parent" android:layout_height="wrap_content" > --> <EditText android:id="@+id/number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:gravity="left|center" android:hint="号码" /> <!-- </LinearLayout> --> <LinearLayout android:layout_width="fill_parent" android:layout_height="60.0dip" android:layout_gravity="bottom" android:layout_marginTop="5dp" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/positiveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_ok_selector" android:gravity="center" /> <Button android:id="@+id/negativeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20.0dip" android:background="@drawable/btn_cancel_selector" android:gravity="center" /> </LinearLayout> </LinearLayout>
java代码
import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * @Function: 自定义对话框 * @Date: 2013-10-28 * @Time: 下午12:37:43 * @author Tom.Cai */ public class CustomDialog extends Dialog { private EditText editText; private Button positiveButton, negativeButton; private TextView title; public CustomDialog(Context context) { super(context,R.style.CustomDialog); setCustomDialog(); } private void setCustomDialog() { View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_normal_layout, null); title = (TextView) mView.findViewById(R.id.title); editText = (EditText) mView.findViewById(R.id.number); positiveButton = (Button) mView.findViewById(R.id.positiveButton); negativeButton = (Button) mView.findViewById(R.id.negativeButton); super.setContentView(mView); } public View getEditText(){ return editText; } @Override public void setContentView(int layoutResID) { } @Override public void setContentView(View view, LayoutParams params) { } @Override public void setContentView(View view) { } /** * 确定键监听器 * @param listener */ public void setOnPositiveListener(View.OnClickListener listener){ positiveButton.setOnClickListener(listener); } /** * 取消键监听器 * @param listener */ public void setOnNegativeListener(View.OnClickListener listener){ negativeButton.setOnClickListener(listener); } }
使用方法:
// 弹窗 private void dialog() { dialog = new CustomDialog(WorkspaceActivity.this); EditText editText = (EditText) dialog.getEditText();//方法在CustomDialog中实现 dialog.setOnPositiveListener(new OnClickListener() { @Override public void onClick(View v) { //dosomething youself dialog2.dismiss(); } }); dialog.setOnNegativeListener(new OnClickListener() { @Override public void onClick(View v) { dialog2.dismiss(); } }); dialog.show(); }