Android之AlertDialog对话框
2017-09-07 19:58
330 查看
主函数:
package com.example.alertdialog;import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
// Button1事件监听
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("您的手机已中毒!!!");
builder.setIcon(R.drawable.icon);
builder.setMessage("请立即前往杀毒");
builder.setPositiveButton("是",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "让你瞎点 中毒了吧",
Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("否",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"十秒钟后自动关机...", Toast.LENGTH_SHORT)
.show();
}
});
//禁止按返回键退出对话框
builder.setCancelable(false);
builder.show();
}
});
// Button2事件监听
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "怂..........",
Toast.LENGTH_LONG).show();
}
});
}
}
--------------------------------------------------------------------
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_gravity="center"
android:text="点我"
android:textSize="130sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="就不点"
android:textSize="35sp"/>
</LinearLayout>
相关文章推荐
- Android Alertdialog之多选对话框
- 简析Android多种AlertDialog对话框效果
- Android:AlertDialog.Builder创建常见对话框
- Android AlertDialog 方法setView(view,0,0,0,0)开发自定义对话框
- Android弹出式对话框AlertDialog中的EditText自动打开软键盘
- 完全自定义Android对话框AlertDialog的实现
- Android漂亮的对话框项目sweet-alert-dialog
- android 开发AlertDialog.builder对话框的实现
- Android AlertDialog实现分享对话框/退出对话框/下载对话框
- Android应用开发学习之AlertDialog对话框
- 【Android】对话框 AlertDialog .(转)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)2010-06-16 18:17:21
- Android中的对话框之二:AlertDialog扩展
- Android 点击AlertDialog上的确定和取消按钮,使对话框不消失
- Android 对话框 (AlertDialog & ProgressDialog)
- Android漂亮的对话框项目sweet-alert-dialog
- Android之对话框AlertDialog及其子类的使用(二)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android: 创建一个AlertDialog对话框,必须按确定或取消按钮才能关闭对话框,禁止按[返回键]或[搜索键]关闭
- android 简单列表对话框(AlertDialog.Builder().setItems())