Android具有了兩種訊息提示的方法,分別是Toast與Notification。本篇是在介紹Toast的使用方式。 Toast是一種快速反應的即時訊息,具有(1)位於應用程式畫面最上層以及(2)不會取得UI focus的兩個特性。 Toast主要可以應用在反應使用者操作圖形介面的回饋,例如某些設定上的成功與否。 另外個人認為利用Toast也可以作為一種在設計程式上debug手段,類似在寫c code時會利用printf的debug方式。
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。而且Toast主要用于向用户显示提示消息。
传参介绍
Toast.makeText(Context context, CharSequence text, @Duration int duration)
contentx : 当前的上下文环境。可用getApplicationContext()或this
text : 要显示的字符串。
duration : 显示的时间长短,以毫秒为单位。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000
显示的几种方式
默认
自定义位置
带图片
完全自定义
页面布局
几个button按钮,方便测试使用
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="默认的Toast"
- android:id="@+id/defaultToastBtn"
- android:textSize="20dp"
- />
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="自定义位置的Toast"
- android:id="@+id/customLocationBtn"
- android:layout_below="@id/defaultToastBtn"
- android:textSize="20dp"
- />
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="带图片的Toast"
- android:id="@+id/imageToastBtn"
- android:layout_below="@id/customLocationBtn"
- android:textSize="20dp"
- />
- <Button android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="完全自定义的Toast"
- android:id="@+id/customToastBtn"
- android:layout_below="@id/imageToastBtn"
- android:textSize="20dp"
- />
複製代碼
完全自定义页面
- <ImageView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- />
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/textView"
- android:layout_below="@id/imageView"
- />
複製代碼
Activity处理
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //绑定监听事件
- findViewById(R.id.defaultToastBtn).setOnClickListener(toastBtn);
- findViewById(R.id.customLocationBtn).setOnClickListener(toastBtn);
- findViewById(R.id.imageToastBtn).setOnClickListener(toastBtn);
- findViewById(R.id.customToastBtn).setOnClickListener(toastBtn);
- }
- View.OnClickListener toastBtn;
- Handler handler;
- {
- toastBtn = new View.OnClickListener() {
- @Override
- //监听事件处理方法
- public void onClick(View v) {
- int what = 0;
- Object obj = "";
- switch (v.getId()) {
- case R.id.defaultToastBtn:
- what = 1;
- obj = "默认的Toast";
- break;
- case R.id.customLocationBtn:
- what = 2;
- obj = "自定义位置的Toast";
- break;
- case R.id.imageToastBtn:
- what = 3;
- obj = "带图片的Toast";
- break;
- case R.id.customToastBtn:
- what = 4;
- obj = "完全自定义的Toast";
- break;
- default:
- what = 0;
- obj = "数据错误";
- break;
- }
- handler.obtainMessage(what, obj).sendToTarget();
- }
- };
- handler = new Handler(Looper.getMainLooper()) {
- @Override
- //消息提示处理
- public void handleMessage(Message message) {
- Toast toast;
- ImageView imageView;
- switch (message.what) {
- case 0:
- Toast.makeText(getApplicationContext(), message.obj.toString(), Toast.LENGTH_SHORT).show();
- break;
- case 1:
- //默认
- Toast.makeText(getApplicationContext(), message.obj.toString(), Toast.LENGTH_SHORT).show();
- break;
- case 2:
- //自定义
- toast = Toast.makeText(getApplicationContext(), message.obj.toString(), Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);// 居中显示
- toast.show();
- break;
- case 3:
- //带图片
- toast = Toast.makeText(getApplicationContext(), message.obj.toString(), Toast.LENGTH_LONG);
- toast.setGravity(Gravity.TOP, 0, 0);// 最上方显示
- LinearLayout toastLayout = (LinearLayout) toast.getView();
- imageView = new ImageView(getApplicationContext());
- imageView.setImageResource(R.drawable.ic_launcher);
- toastLayout.addView(imageView, 0);// 0 图片在文字的上方 , 1 图片在文字的下方
- toast.show();
- break;
- case 4:
- //完全自定义,需要创建一个xml模板
- LayoutInflater inflater = getLayoutInflater();// LayoutInflater对象
- View layout = inflater.inflate(R.layout.custom_view, null);
- imageView = (ImageView) layout.findViewById(R.id.imageView);
- TextView text = (TextView) layout.findViewById(R.id.textView);
- imageView.setImageResource(R.drawable.ic_launcher);
- text.setText(message.obj.toString());
- toast = new Toast(getApplicationContext());
- // 底部 、水平居中,X偏移50 Y偏移50
- toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 50, 50);
- toast.setDuration(Toast.LENGTH_SHORT);
- toast.setView(layout);
- toast.show();
- break;
- }
- }
- };
- }
- }
複製代碼
|