Android 7 自定义Toast显示时间 通过longclick触发,显示100秒
时间: 2023-12-03 11:44:12 浏览: 43
要实现自定义Toast显示时间,需要自定义一个Toast,然后在其中设置显示时间。下面是一个示例代码:
```java
public class CustomToast {
private static final int LONG_DURATION = 100000;
public static void showLong(Context context, CharSequence message) {
Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toast.setDuration(LONG_DURATION);
toast.show();
}
}
```
然后,在长按事件中调用该自定义Toast来显示:
```java
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
CustomToast.showLong(MainActivity.this, "长按事件触发了");
return true;
}
});
```
其中,`LONG_DURATION`是设置的Toast显示时间,单位为毫秒。在这个例子中,我设置了100秒。你可以根据需要进行调整。
相关问题
Android 7.0.2自定义Toast显示时间 通过longclick触发,显示100秒
要实现自定义Toast显示时间,可以通过以下步骤进行:
1. 创建一个自定义的Toast布局文件,比如命名为custom_toast.xml。
2. 在自定义布局文件中添加一个TextView,并设置其显示内容。
3. 在Java代码中实例化该自定义布局文件,并设置其显示时间。
4. 在长按事件中触发该自定义Toast显示。
具体的代码实现如下:
1. 创建自定义Toast布局文件custom_toast.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#88000000"
android:orientation="horizontal"
android:padding="20dp">
<TextView
android:id="@+id/custom_toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
```
2. 在Java代码中实例化该自定义布局文件,并设置其显示时间。
```
// 实例化自定义布局文件
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, null);
// 设置Toast显示的文字内容
TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText("自定义Toast显示100秒");
// 设置Toast的显示时间为100秒
int toastDurationInMilliSeconds = 100000;
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
```
3. 在长按事件中触发该自定义Toast显示。
```
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// 在长按事件中触发自定义Toast显示
// 实例化自定义布局文件
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, null);
// 设置Toast显示的文字内容
TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText("自定义Toast显示100秒");
// 设置Toast的显示时间为100秒
int toastDurationInMilliSeconds = 100000;
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
return true;
}
});
```
这样就可以在长按事件中触发一个自定义Toast,并设置其显示时间为100秒了。
Android 7.0.2自定义Toast显示时间 通过longclick触发,显示200秒
要实现自定义的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秒。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)