close
- Toast 是一個消息的提示元件
- 可以隨意設定想要顯示的位置
- 自定義的內容 ( Ex : 圖片 )
- 簡單的封裝
Step1 . Xml
<Button android:id="@+id/btn_toast_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="默認"/> <Button android:id="@+id/btn_toast_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="改變位置"/> <Button android:id="@+id/btn_toast_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="帶圖片(自定義)"/> <Button android:id="@+id/btn_toast_4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ToastUtil" android:textAllCaps="false"/>
Step2.
package vision.google.com.camera; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import util.ToastUtil; public class ToastActivity extends AppCompatActivity { private Button mBtnToast1,mBtnToast2,mBtnToast3,mBtnToast4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toast); //Default mBtnToast1 = (Button)findViewById(R.id.btn_toast_1); //change position mBtnToast2 = (Button)findViewById(R.id.btn_toast_2); //with photo mBtnToast3 = (Button)findViewById(R.id.btn_toast_3); mBtnToast4 = (Button)findViewById(R.id.btn_toast_4); OnClick onClick = new OnClick(); mBtnToast1.setOnClickListener(onClick); mBtnToast2.setOnClickListener(onClick); mBtnToast3.setOnClickListener(onClick); mBtnToast4.setOnClickListener(onClick); } class OnClick implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_toast_1: Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show(); break; case R.id.btn_toast_2: Toast toastCenter = Toast.makeText(getApplicationContext(),"Toast Center",Toast.LENGTH_LONG); toastCenter.setGravity(Gravity.CENTER,0,0); toastCenter.show(); break; case R.id.btn_toast_3: Toast toastCustom = new Toast(getApplicationContext()); LayoutInflater inflater = LayoutInflater.from(ToastActivity.this); View view = inflater.inflate(R.layout.layout_toast,null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_toast); TextView textView = (TextView) view.findViewById(R.id.tv_toast); imageView.setImageResource(R.drawable.flyfoxy); textView.setText("Foxy ball ball"); toastCustom.setView(view); toastCustom.setDuration(Toast.LENGTH_LONG); toastCustom.show(); break; case R.id.btn_toast_4: ToastUtil.showMsg(getApplicationContext(),"Toast Util"); break; } } } }
Step3. (封裝類)
package util; import android.content.Context; import android.widget.Toast; /** * Created by ericpeng on 2018/3/20. */ public class ToastUtil { public static Toast mToast; public static void showMsg(Context context, String msg) { if (mToast == null) { mToast = Toast.makeText(context,msg,Toast.LENGTH_LONG); } else { mToast.setText(msg); } mToast.show(); } }
全站熱搜
留言列表