Android 7.0.2自定义Toast显示时间 通过longclick触发,显示200秒
时间: 2023-11-29 17:05:23 浏览: 87
要实现自定义的Toast显示时间,可以通过以下步骤:
1. 创建一个自定义的Toast布局文件,例如toast_custom.xml,可以在其中添加任意的视图组件。
2. 在代码中获取该布局文件并创建Toast对象,例如:
```
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom, (ViewGroup) findViewById(R.id.toast_layout_root));
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
```
3. 在长按事件的回调函数中设置Toast的显示时间为200秒,例如:
```
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast toast = ... // 创建Toast对象
toast.setDuration(200000); // 设置显示时间为200秒
toast.show();
return true;
}
});
```
这样就可以实现自定义的Toast显示时间,通过长按触发显示,并且显示时间为200秒。
阅读全文
相关推荐














