本文的dialog弹框为固定格式,可以完美复制,通俗易懂,帮助学习,也可以作为例子,复制,直接拿来用,本人亲测
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出对话框"
android:onClick="showProgress" />
</LinearLayout>
MainActivity.java文件:
package com.example.zdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity{
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getDialog();
}
});
}
public void getDialog(){
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setMessage("点击返回将中断找回密码,是否返回");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
MainActivity.this.finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
记住本文还有一个重要知识点,dialog和onClick的点击事件会有一个冲突,我在dialog点击事件前面做了一个声明,大家以后遇到类似的包文件冲突,可以按这种思路来做