Android消息提示框Toast

Android消息提示框Toast
Toast是Android中一种简易的消息提示框。和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,toast会根据用户设置的显示时间后自动消失。
创建Toast的方法总共有2种:
1.Toast.makeText(Context context, (CharSequence text)/( int resId), int duration)
参数:context是指上下文对象,通常是当前的Activity,text是指自己写的消息内容,resId指显示内容引用Resource的那条数据,duration指Toast的显示时间,Toast默认有LENGTH_SHORT和LENGTH_LONG两常量,分别表示时间长和短,也可以自定义时间,如:5000表示显示5秒。用此方法获得Toast对象之后调用.show()方法即可显示消息。
2.自定义Toast,通过Toast toast = new Toast(Context context)获得Toast对象,调用.show()方法即可显示消息。
toast.setDuration(duration)设置显示时间,
toast.setGravity(gravity, xOffset, yOffset)设置显示位置,三个参数分别表示(起点位置,水平位移,垂直位移),
toast.setMargin(float horizontalMargin, float verticalMargin)以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下)
toast.setText(text)设置消息内容,
toast.setView(view)设置Toast显示的视图组件。

实例:Android消息提示框Toast_第1张图片Android消息提示框Toast_第2张图片Android消息提示框Toast_第3张图片
main.xml



    

    

    

    

    


custom.xml



    

    

        

        
    


MainActivity.java
public class MainActivity extends Activity implements OnClickListener {

	private Button simpleToastBtn, simpleToastWithCustomPositionBtn,
			imageToastBtn, customToastBtn, runToastFromOtherThreadBtn;
	Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		simpleToastBtn = (Button) findViewById(R.id.simpleToast);
		simpleToastWithCustomPositionBtn = (Button) findViewById(R.id.simpleToastWithCustomPosition);
		imageToastBtn = (Button) findViewById(R.id.imageToast);
		customToastBtn = (Button) findViewById(R.id.customToast);
		runToastFromOtherThreadBtn = (Button) findViewById(R.id.runToastFromOtherThread);

		simpleToastBtn.setOnClickListener(this);
		simpleToastWithCustomPositionBtn.setOnClickListener(this);
		imageToastBtn.setOnClickListener(this);
		customToastBtn.setOnClickListener(this);
		runToastFromOtherThreadBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Toast toast = null;
		switch (v.getId()) {
		case R.id.simpleToast:
			Toast.makeText(MainActivity.this, "默认Toast样式", Toast.LENGTH_LONG)
					.show();
			break;

		case R.id.simpleToastWithCustomPosition:
			toast = Toast.makeText(MainActivity.this, "自定义位置Toast",
					Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;

		case R.id.imageToast:
			toast = Toast.makeText(MainActivity.this, "带图片的Toast",
					Toast.LENGTH_LONG);
			LinearLayout layout = (LinearLayout) toast.getView();
			ImageView image = new ImageView(MainActivity.this);
			image.setImageResource(R.drawable.ic_launcher);
			layout.addView(image, 0);
			toast.show();
			break;

		case R.id.customToast:
			LayoutInflater inflater = getLayoutInflater();
			View view = inflater.inflate(R.layout.custom, null);

			ImageView customImage = (ImageView) view
					.findViewById(R.id.customImageToast);
			TextView customText = (TextView) view
					.findViewById(R.id.customTextToast);
			customImage.setImageResource(R.drawable.ic_launcher);
			customText.setText("完全自定义Toast");

			toast = new Toast(MainActivity.this);
			toast.setGravity(Gravity.RIGHT | Gravity.TOP, 30, 30);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(view);
			toast.show();
			break;

		case R.id.runToastFromOtherThread:
			new Thread(new Runnable() {
				public void run() {
					showToast();
				}
			}).start();
			break;
		}
	}

	public void showToast() {
		handler.post(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(MainActivity.this, "我来自其他线程", Toast.LENGTH_LONG)
						.show();
			}
		});
	}
}



你可能感兴趣的:(Android学习)