android toast快捷键,android toast使用方法

1.默认展示

copycode.gif// 第一个参数:当前的上下文环境。可用getApplicationContext()或this 

// 第二个参数:要显示的字符串。也可是R.string中字符串ID

// 第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms

Toast toast=Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT);

//显示toast信息

toast.show();

copycode.gif

2.自定义显示位置

copycode.gifToast toast=Toast.makeText(getApplicationContext(), "自定义显示位置的Toast", Toast.LENGTH_SHORT); 

//第一个参数:设置toast在屏幕中显示的位置。我现在的设置是居中靠顶

//第二个参数:相对于第一个参数设置toast位置的横向X轴的偏移量,正数向右偏移,负数向左偏移

//第三个参数:同的第二个参数道理一样

//如果你设置的偏移量超过了屏幕的范围,toast将在屏幕内靠近超出的那个边界显示

toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);

//屏幕居中显示,X轴和Y轴偏移量都是0

//toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

copycode.gif

3.带图片的

copycode.gifToast toast=Toast.makeText(getApplicationContext(), "显示带图片的toast", 3000); 

toast.setGravity(Gravity.CENTER, 0, 0);

//创建图片视图对象

ImageView p_w_picpathView= new ImageView(getApplicationContext());

//设置图片

p_w_picpathView.setImageResource(R.drawable.ic_launcher);

//获得toast的布局

LinearLayout toastView = (LinearLayout) toast.getView();

//设置此布局为横向的

toastView.setOrientation(LinearLayout.HORIZONTAL);

//将ImageView在加入到此布局中的第一个位置

toastView.addView(p_w_picpathView, 0);

copycode.gif

4.完全自定义显示方式

1. toast.xml布局

copycode.gif

android:id="@+id/p_w_picpath"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/content"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

copycode.gif

2.activity代码

copycode.gifpublic class ToastActivity extends Activity {    private Button bt;    private ImageView p_w_picpath;    private TextView title, content;

@Override    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

bt = (Button) findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener() {

@Override            public void onClick(View v) {

showToast();

}

});

}    private void showToast() {

LayoutInflater inflater = getLayoutInflater();

View view = inflater.inflate(R.layout.toast, null);

p_w_picpath = (ImageView) view.findViewById(R.id.p_w_picpath);

title = (TextView) view.findViewById(R.id.title);

content = (TextView) view.findViewById(R.id.content);

p_w_picpath.setBackgroundResource(R.drawable.ic_launcher);

title.setText("自定义toast");

content.setText("hello,self toast");

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.CENTER, 0, 0);

toast.setDuration(Toast.LENGTH_SHORT);

toast.setView(view);

toast.show();

}

}

copycode.gif

5.其他线程通过Handler的调用

copycode.gif//调用方法1 

//Thread th=new Thread(this);

//th.start();

//调用方法2

handler.post(new Runnable() {

@Override

public void run() {

showToast();

}

});

Java代码public void showToast(){

Toast toast=Toast.makeText(getApplicationContext(), "Toast在其他线程中调用显示", Toast.LENGTH_SHORT);

toast.show();

}

Java代码

Handler handler=new Handler(){

@Override

public void handleMessage(Message msg) {

int what=msg.what;

switch (what) {

case 1:

showToast();

break;

default:

break;

}

super.handleMessage(msg);

}

};

Java代码

@Override

public void run() {

handler.sendEmptyMessage(1);

}

copycode.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值