Toast是一种不影响用户操作的通知,出现一定时间自动消失。一般用户通知用户已经刷新内容。
原始类:
package com.xinbo.templete;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
//一个按钮事件,需要在xml里拉个按钮
public void TOAST(View v) {
Toast result = new Toast(this);
//.用以判断当连续多次toast,原先未及时toast的会被取消而马上toast下一个。////最后不要忘记result.show().
if(result!=null){
result.cancel();
}
LayoutInflater inflate = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//R.layout.custom_toast,是自定义的布局,这个布局的个性设置由我们决定,设置颜色、背景、文字字体等等
View v2 = inflate.inflate(R.layout.custom_toast, null);
TextView tv = (TextView) v2.findViewById(R.id.textView111);
tv.setText("toast....");
result.setGravity(Gravity.TOP, 20, 50);
// result.setGravity(gravity, xOffset, yOffset)
result.setView(v2);
result.setDuration(Toast.LENGTH_SHORT);
result.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
//custom_toast的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="12dp"
android:layout_height="wrap_content"
android:background="@drawable/abs_bg" >
<TextView
android:id="@+id/textView111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Medium Text"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
当然也可以经过封装,只要在需要的时候调用、传参数(上下文,显示的内容、显示时间)即可
封装类:
package com.xinbo.lib_yuchen;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public final class ToastUtils {
private static Toast result;
public static void showToast(Context context, String msg, int duration)
{
if (result != null)
{
result.cancel();
}
result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.custom_toast, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(msg);
result.setView(v);
result.setGravity(Gravity.TOP, 20, 20);
result.setDuration(Toast.LENGTH_SHORT);
result.show();
}
private ToastUtils(){}
}
测试类:关键的一句: ToastUtils.showToast(this, "定制Toast", Toast.LENGTH_SHORT);
package com.xinbo.templete;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import com.xinbo.lib_yuchen.ToastUtils;
public class MainActivity extends Activity {
public void btnToast(View v1) {
ToastUtils.showToast(this, "定制Toast", Toast.LENGTH_SHORT);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}