自定义Toast
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。而且Toast主要用于向用户显示提示消息.
#默认效果
mbutton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String path = mEditText.getText().toString();
if(path.equals("") ){
Toast.makeText(MainActivity.this,"网址不能为空", Toast.LENGTH_SHORT).show();
}
}
});
@Override
public void onClick(View v) {
String path = mEditText.getText().toString();
if(path.equals("") ){
Toast.makeText(MainActivity.this,"网址不能为空", Toast.LENGTH_SHORT).show();
}
}
});
#自定义效果
inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout .findViewById(R.id.tvImageToast);//设置图片
image.setImageResource(R.drawable.icon); //设置标题
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention"); //设置文字
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); //设置位置
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
ImageView image = (ImageView) layout .findViewById(R.id.tvImageToast);//设置图片
image.setImageResource(R.drawable.icon); //设置标题
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention"); //设置文字
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); //设置位置
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
#其他线程显示
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();