首页 / 操作系统 / Linux / Android中AlertDialog和Toast的使用
1、AlertDialog是一个信息提示框,当出现是,需要用户点击,才会消失 2、Toast也是一个信息提示框,出现后会更具设定的时间,自动消失 实例代码:3、布局文件 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="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/alert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Make an alert"></Button> <Button android:id="@+id/toast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Make a toast"></Button> </LinearLayout>2、java代码 package yyl.message; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MessageActivity extends Activity { //定义变量 private Button alert = null; private Button toast = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据Id得到控件对象 alert = (Button)findViewById(R.id.alert); toast = (Button)findViewById(R.id.toast); //给按钮设定单击事件监听器 alert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显示AlertDialog new AlertDialog.Builder(MessageActivity.this).setTitle("MessageDemo").setMessage("Err").setNeutralButton("close", new DialogInterface.OnClickListener() { //点击AlertDialog上的按钮的事件处理代码 @Override public void onClick(DialogInterface dialog, int which) { System.out.println("yangyulin"); } }).show(); } }); toast.setOnClickListener(new View.OnClickListener() { //显示Toast @Override public void onClick(View v) { Toast.makeText(MessageActivity.this, "<Clink,Clink>", Toast.LENGTH_SHORT).show(); } }); } }
收藏该网址