package com.example.dialogself;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
btn = (Button) findViewById(R.id.btn);
// 2. 新建对话框对象
AlertDialog.Builder dialogs = new AlertDialog.Builder(MainActivity.this);
final AlertDialog dialog = dialogs.create();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popDialog(dialog);
//消息内容
TextView dialog_msg = (TextView)dialog.findViewById(R.id.text2);
dialog_msg.setText("你确定吗?");
//确定按钮
TextView btnOK = (TextView)dialog.findViewById(R.id.textView1);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "您点击了ok",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
// 取消按钮
TextView btnCancel = (TextView) dialog.findViewById(R.id.textView2);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "您点击了cancel",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
});
}
private void popDialog(final Dialog dialog) {
Window dialogWindow = dialog.getWindow();;
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
dialogWindow.setGravity(Gravity.CENTER);
dialog.setCancelable(false);
dialog.show();
dialogWindow.setContentView(R.layout.dialog);
p.height = (int) (getResources().getDisplayMetrics().heightPixels * 0.6);
p.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.65);
dialogWindow.setAttributes(p);
}
}
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
btn = (Button) findViewById(R.id.btn);
// 2. 新建对话框对象
AlertDialog.Builder dialogs = new AlertDialog.Builder(MainActivity.this);
final AlertDialog dialog = dialogs.create();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popDialog(dialog);
//消息内容
TextView dialog_msg = (TextView)dialog.findViewById(R.id.text2);
dialog_msg.setText("你确定吗?");
//确定按钮
TextView btnOK = (TextView)dialog.findViewById(R.id.textView1);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "您点击了ok",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
// 取消按钮
TextView btnCancel = (TextView) dialog.findViewById(R.id.textView2);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "您点击了cancel",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
});
}
private void popDialog(final Dialog dialog) {
Window dialogWindow = dialog.getWindow();;
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
dialogWindow.setGravity(Gravity.CENTER);
dialog.setCancelable(false);
dialog.show();
dialogWindow.setContentView(R.layout.dialog);
p.height = (int) (getResources().getDisplayMetrics().heightPixels * 0.6);
p.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.65);
dialogWindow.setAttributes(p);
}
}