用Android Studio实现消息提示Toast的代码,目标如下:1.通过点击三个按钮分别实现Toast的默认方式、自定义方式、带图标方式3种显示方式。2.用图片作为消息提示的图标。3.不需要用到自定义布局文件。
时间: 2024-05-06 09:20:17 浏览: 6
在布局文件中添加三个按钮:
```
<Button
android:id="@+id/default_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Toast" />
<Button
android:id="@+id/custom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toast" />
<Button
android:id="@+id/icon_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Icon Toast" />
```
在Java代码中设置按钮点击事件,并在事件处理函数中实现Toast的三种方式:
```
Button defaultBtn = findViewById(R.id.default_btn);
defaultBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Default Toast", Toast.LENGTH_SHORT).show();
}
});
Button customBtn = findViewById(R.id.custom_btn);
customBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = layout.findViewById(R.id.text);
text.setText("Custom Toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
});
Button iconBtn = findViewById(R.id.icon_btn);
iconBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(MainActivity.this, "Icon Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.icon);
toast.setView(imageView);
toast.show();
}
});
```
其中,Custom Toast需要先编写一个自定义布局文件:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:background="#DADADA">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:text="Custom Toast"/>
</LinearLayout>
```
其中,@drawable/icon是要用作图标的图片资源。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)