Toast:相信大家都不会陌生啦!在开发中我们要给用户友好提示时,会经常使用到。今天让我们来看看这个库给我们带来:错误提示,信息提示,成功提示,警告提示等等。请看效果图:
1,在build.gradle文件中添加如下代码:
2,在app/build.gradle文件中添加如下代码:
compile 'com.github.GrenderG:Toasty:1.1.3'记得sync Now。
3,布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.classical.newtoasty.MainActivity"> <Button android:id="@+id/btn_error_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Error_show"/> <Button android:id="@+id/btn_success_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="success_show"/> <Button android:id="@+id/btn_info_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="info_show"/> <Button android:id="@+id/btn_warning_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="warn_show"/> <Button android:id="@+id/btn_usual_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="usual_show"/> <Button android:id="@+id/btn_usual_icon_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="usual_icon_show"/></LinearLayout>4,在MainActivity.java中使用:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mError,mSuccess,mInfo,mWarn,mUsual,mUsual_out; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mError =(Button)findViewById(R.id.btn_error_toast); mSuccess =(Button)findViewById(R.id.btn_success_toast); mInfo =(Button)findViewById(R.id.btn_info_toast); mWarn =(Button)findViewById(R.id.btn_warning_toast); mUsual =(Button)findViewById(R.id.btn_usual_toast); mUsual_out =(Button)findViewById(R.id.btn_usual_icon_toast); mError.setOnClickListener(this); mSuccess.setOnClickListener(this); mInfo.setOnClickListener(this); mWarn.setOnClickListener(this); mUsual.setOnClickListener(this); mUsual_out.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_error_toast : Toasty.error(this,"您的输入有误!", Toast.LENGTH_SHORT,true).show(); break; case R.id.btn_success_toast: Toasty.success(this,"登入成功!",Toast.LENGTH_SHORT,true).show(); break; case R.id.btn_info_toast: Toasty.info(this, "信息输入", Toast.LENGTH_SHORT, true).show(); break; case R.id.btn_warning_toast: Toasty.warning(this, "警告!", Toast.LENGTH_SHORT, true).show(); break; case R.id.btn_usual_toast : Toasty.normal(this, "普通").show(); break; case R.id.btn_usual_icon_toast : Drawable icon = getResources().getDrawable(R.drawable.logo); Toasty.normal(this, "普通+图片",icon).show(); //你还可以自定义字体颜色,背景颜色等查看下源码,填入相应的参数就OK啦。 Toasty.custom(this, "自定义吐司", icon, Color.GRAY, Color.BLUE, Toast.LENGTH_SHORT,true, true).show(); break; } }}新闻热点
疑难解答