Dialog
package com.example.dialogdemol; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 显示标准的对话框 * @param view */ public void btnDialog(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setPositiveButton("確定", listener); builder.setNegativeButton("取消", listener); // 设置 点击返回键或者对话框之外的区域能否让对话框消失 // 即是否能够中断对话框 builder.setCancelable(false); builder.show(); } private DialogInterface.OnClickListener listener = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: Toast.makeText(getApplicationContext(), "确定点击", Toast.LENGTH_SHORT).show(); break; case DialogInterface.BUTTON_NEGATIVE: Toast.makeText(getApplicationContext(), "取消点击", Toast.LENGTH_SHORT).show(); break; default: break; } } }; /** * 自定义Dialog * * @param view */ @SuppressLint("InflateParams") public void btnMyDialog(View view) { final AlertDialog dialog = new AlertDialog.Builder(this).create(); LayoutInflater inflater = LayoutInflater.from(this); View dialogView = inflater.inflate(R.layout.dialog_layout, null); final EditText editText = (EditText) dialogView.findViewById(R.id.editText); Button btnOk = (Button) dialogView.findViewById(R.id.btnOK); btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), editText.getText().toString().trim(), Toast.LENGTH_SHORT) .show(); ; dialog.dismiss(); } }); dialog.setView(dialogView); dialog.show(); } }