概述
Toast是Android系统提供的一种提醒方式,在程序中可以使用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间。
makeText方法的签名及参数介绍
public class Toast {
public static Toast makeText(Context context, CharSequence text, @Duration int duration)
//Make a standard toast that just contains a text view.
//@param context: The context to use
//@param text: The text to show
//@param duration: How long to display the message
public static Toast makeText(Context context, @StringRes int resId, @Duration int duration) throws Resources.NotFoundException
//Make a standard toast that just contains a text view with the text from a resource.
//@param context: The context to use
//@param resId: The resource id of the string resource to use
//@param duration: How long to display the message
//@throw: If the resource can't be found
}
实例
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//通过Toast.makeText方法创建一个Toast对象,然后调用show()将其显示出来
Toast.makeText(FirstActivity.this, "You click button 1", Toast.LENGTH_SHORT).show();
}
});
}
}
参考《第一行代码——Android》