Android 模式对话框提示Dialog
Posted 幽冥狂_七
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 模式对话框提示Dialog相关的知识,希望对你有一定的参考价值。
1、先写一个Dialog类
CustomDialog
package com.example.heng.adtest; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.Gravity; import android.widget.Toast; public class CustomDialog { //声明一个Diaglog private AlertDialog.Builder builder; //上下文对象 private Context context; public CustomDialog(Context cnt) { this.context = cnt; } //声明弹出框方法 //buttontext :按钮名称 //title :标题 //message:信息 //callbock:返回信息 public void CreateDialog(String buttontext, String title, String message, final CallBack callbock){ builder = new AlertDialog.Builder(context); builder.setTitle(title); builder.setMessage(message); //添加确定按钮 builder.setPositiveButton(buttontext, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //业务逻辑判断 callbock.isConfirm(true); } }); builder.create().show(); } public interface CallBack{ public void isConfirm(boolean flag); } }
2、在MainActivity中调用
package com.example.heng.adtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button buttonA; private Button buttonB; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonA = (Button) this.findViewById(R.id.button1); buttonB = (Button) this.findViewById(R.id.button2); buttonA.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CustomDialog dialog = new CustomDialog(MainActivity.this); dialog.CreateDialog("确定", "提示", "确定要删除吗?", new CustomDialog.CallBack() { @Override public void isConfirm(boolean flag) { System.out.println("---------->>"+flag); if (flag){ } } }); } }); buttonB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); } }
3、页面内容
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.heng.adtest.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dialog提示" android:id="@+id/button1" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="38dp" android:layout_marginStart="38dp" android:layout_marginTop="46dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_toRightOf="@+id/button1" android:layout_alignLeft="@+id/button1" android:layout_alignStart="@+id/button1" android:layout_marginTop="159dp" android:text="普通提示" /> </RelativeLayout>
以上是关于Android 模式对话框提示Dialog的主要内容,如果未能解决你的问题,请参考以下文章