Android自定义对话框
hello_1s 人气:01、定义对话框的布局
<?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"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="16sp" android:layout_margin="4dp" android:text="标题"/> <TextView android:id="@+id/content1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text="第一行文字" android:layout_margin="4dp" android:layout_below="@id/title" android:gravity="center"/> <TextView android:id="@+id/content2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text="第一行文字" android:layout_margin="4dp" android:layout_below="@id/content1" android:gravity="center"/> <LinearLayout android:id="@+id/linear" android:layout_below="@id/content2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="6dp" android:paddingRight="20dp" android:paddingLeft="20dp" > <Button android:id="@+id/ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="14sp" android:text="确定"/> <Button android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="14sp" android:text="取消"/> </LinearLayout> <Button android:id="@+id/know" android:layout_below="@id/linear" android:gravity="center" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text="知道了"/> </RelativeLayout>
对话框样式(比较丑哈,就是大概这个意思,嘿嘿)
2、定义接口
利用接口回调的方式使对话框消失。
public interface DialogListener { void onClick(MyDialog dialog,View view); }
3、写一个类继承Dialog,并重写构造方法
说明:第三个按钮的监听与其他两个不同,前两个使用的是button原声的监听事件,第三个为自定义的接口,目的是获取MyDialog,然后通过dismiss()方法使对话框不显示。(接口回调的方式)
public class MyDialog extends Dialog { private TextView mTipOneView; private TextView mTipTwoView; private TextView mTitleView; private Button mOkView; private Button mCancelView; private Button mKonwView; private View.OnClickListener mOkListener; private View.OnClickListener mCancelListener; private DialogListener mKnowListener; private String title; private String oneTip; private String twoTip; private void setOnDialogListener(DialogListener listener){ this.mKnowListener = listener; } public MyDialog(Context context) { super(context); } public MyDialog(Context context,String title,String oneTip,String twoTip,View.OnClickListener ok,View.OnClickListener cancel,DialogListener know) { this(context); this.title = title; this.oneTip = oneTip; this.twoTip = twoTip; mOkListener = ok; mCancelListener = cancel; mKnowListener = know; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_dialog); mCancelView = (Button) findViewById(R.id.cancel); mOkView = (Button) findViewById(R.id.ok); mKonwView = (Button) findViewById(R.id.know); mTipOneView = (TextView) findViewById(R.id.content1); mTipTwoView = (TextView) findViewById(R.id.content2); mTitleView = (TextView) findViewById(R.id.title); mTitleView.setText(title); mTipTwoView.setText(twoTip); mTipOneView.setText(oneTip); mCancelView.setOnClickListener(mCancelListener); mOkView.setOnClickListener(mOkListener); mKonwView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mKnowListener.onClick(MyDialog.this,view); } }); } }
通过setViewContent(R.layout.~)为对话框设置样式;使用构造方法传值。
4、显示对话框
public class CustomDialogActivity extends AppCompatActivity { private DialogListener listener; private MyDialog myDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_dialog); listener = new DialogListener() { @Override public void onClick(MyDialog dialog, View view) { myDialog.dismiss(); } }; } public void showDialog(View view){ myDialog = new MyDialog(CustomDialogActivity.this, "不知道", "有问题么", "啥问题", new View.OnClickListener() { @Override public void onClick(View view) { Log.e("----->", "ok"); //点击按钮发生的事件 } }, new View.OnClickListener() { @Override public void onClick(View view) { Log.e("----->", "cancle"); //点击按钮发生的事件 } },listener); myDialog.show(); } }
注意:一定不要忘了show(),否则对话框不显示。
加载全部内容