Android中AlertDialog和Toast的使用
2010-11-25 20:53
423 查看
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();
}
});
}
}
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();
}
});
}
}
相关文章推荐
- Android中AlertDialog和Toast的使用
- Android开发2(Toast,AlertDialog,Snackbar,TextInputLayout的使用)
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
- Android开发中提示框Toast、AlertDialog的使用!
- APP开发实战150-Android 提示功能设计(AlertDialog、Toast和Snackbar的使用)
- Android中AlertDialog和Toast的使用
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
- Android使用AlertDialog实现对话框
- android使用Toast时出现的错误
- Android中的AlertDialog使用示例二(普通选项对话框)
- Android 弹出通知Toast的使用
- Android中对于没有Looper的类,要使用Toast的问题
- Android Toast几种使用方法:附源码
- Android 小项目之---Toast对象详细使用
- Android--Toast使用介绍
- android的控件Toast和EditText的使用方法
- Android AlertDialog和PopupWindow使用和区别
- 使用AlertDialog实现使用在android中弹出对话框,询问用户是否继续执行
- Android官方推荐使用DialogFragment替换AlertDialog
- Android应用开发学习—Toast使用方法大全