toast问题

chengzhenyu преди 8 години
родител
ревизия
bd65791c98
променени са 3 файла, в които са добавени 88 реда и са изтрити 11 реда
  1. 12 11
      app/src/main/java/ai/pai/client/activity/PhotoDetailsActivity.java
  2. 56 0
      app/src/main/java/ai/pai/client/utils/ToastUtils.java
  3. 20 0
      app/src/main/res/layout/custom_toast.xml

+ 12 - 11
app/src/main/java/ai/pai/client/activity/PhotoDetailsActivity.java

@@ -51,6 +51,7 @@ import ai.pai.client.utils.GroupCreateUtils;
51 51
 import ai.pai.client.utils.HttpPostTask;
52 52
 import ai.pai.client.utils.PhotoLoader;
53 53
 import ai.pai.client.utils.SystemUtils;
54
+import ai.pai.client.utils.ToastUtils;
54 55
 import ai.pai.client.utils.UmengEvent;
55 56
 import ai.pai.client.utils.UrlContainer;
56 57
 import ai.pai.client.views.CountView;
@@ -417,14 +418,14 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
417 418
 
418 419
     private void clickPay(float fee,String type){
419 420
         if(!NetworkUtil.isNetworkConnected(this)){
420
-            Toast.makeText(this,R.string.network_disconnect,Toast.LENGTH_SHORT).show();
421
+            ToastUtils.showShortToast(this,R.string.network_disconnect);
421 422
             return;
422 423
         }
423 424
         if (!isPayListenerRegistered) {
424 425
             PayListenerManager.getInstance().putPayResultListener("play",this);
425 426
             isPayListenerRegistered = true;
426 427
         }
427
-        Toast.makeText(this,R.string.pay_create_order,Toast.LENGTH_SHORT).show();
428
+        ToastUtils.showShortToast(this,R.string.pay_create_order);
428 429
         doWXPayTask(fee,type);
429 430
     }
430 431
 
@@ -433,7 +434,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
433 434
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
434 435
         if(position==photoList.size()-1){
435 436
             if(fetchPhotosTask!=null && fetchPhotosTask.getStatus()== AsyncTask.Status.RUNNING){
436
-                Toast.makeText(this,R.string.please_wait,Toast.LENGTH_SHORT).show();
437
+                ToastUtils.showShortToast(this,R.string.please_wait);
437 438
             }
438 439
         }
439 440
     }
@@ -610,7 +611,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
610 611
             protected void onPostFail(Context context) {
611 612
                 super.onPostFail(context);
612 613
                 if(!TextUtils.isEmpty(toastMsg)){
613
-                    Toast.makeText(context,toastMsg,Toast.LENGTH_SHORT).show();
614
+                    ToastUtils.showShortToast(context,toastMsg);
614 615
                 }
615 616
             }
616 617
 
@@ -618,7 +619,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
618 619
             protected void onPostSuccess(Context context) {
619 620
                 super.onPostSuccess(context);
620 621
                 if(!TextUtils.isEmpty(toastMsg)){
621
-                    Toast.makeText(context,toastMsg,Toast.LENGTH_SHORT).show();
622
+                    ToastUtils.showShortToast(context,toastMsg);
622 623
                 }
623 624
                 jumpWhenPaid();
624 625
             }
@@ -674,7 +675,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
674 675
             @Override
675 676
             protected void onPostFail(Context context) {
676 677
                 super.onPostFail(context);
677
-                Toast.makeText(getApplicationContext(), R.string.create_order_fail, Toast.LENGTH_SHORT).show();
678
+                ToastUtils.showShortToast(context,R.string.create_order_fail);
678 679
             }
679 680
 
680 681
             @Override
@@ -788,7 +789,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
788 789
             @Override
789 790
             protected void onPostFail(Context context) {
790 791
                 super.onPostFail(context);
791
-                Toast.makeText(context,R.string.get_price_fail,Toast.LENGTH_SHORT).show();
792
+                ToastUtils.showShortToast(context,R.string.get_price_fail);
792 793
             }
793 794
 
794 795
             @Override
@@ -956,7 +957,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
956 957
 
957 958
     private void doPhotoThumbUpTask() {
958 959
         if(currentPhotoItem.thumbUp){
959
-            Toast.makeText(this,R.string.already_thumb_up,Toast.LENGTH_SHORT).show();
960
+            ToastUtils.showShortToast(this,R.string.already_thumb_up);
960 961
             return;
961 962
         }
962 963
         if(currentPhotoItem.photoId.equals(processingThumbPhotoId)){
@@ -992,7 +993,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
992 993
             protected void onPostFail(Context context) {
993 994
                 super.onPostFail(context);
994 995
                 processingThumbPhotoId = null;
995
-                Toast.makeText(getApplicationContext(), R.string.photo_like_fail, Toast.LENGTH_SHORT).show();
996
+                ToastUtils.showShortToast(getApplicationContext(),R.string.photo_like_fail);
996 997
             }
997 998
 
998 999
             @Override
@@ -1041,7 +1042,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
1041 1042
             @Override
1042 1043
             protected void onPostFail(Context context) {
1043 1044
                 super.onPostFail(context);
1044
-                Toast.makeText(getApplicationContext(), R.string.photo_comment_fail, Toast.LENGTH_SHORT).show();
1045
+                ToastUtils.showShortToast(getApplicationContext(),R.string.photo_comment_fail);
1045 1046
             }
1046 1047
 
1047 1048
             @Override
@@ -1065,7 +1066,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
1065 1066
             case BaseResp.ErrCode.ERR_COMM:
1066 1067
             case BaseResp.ErrCode.ERR_SENT_FAILED:
1067 1068
             case BaseResp.ErrCode.ERR_UNSUPPORT:
1068
-                Toast.makeText(this,R.string.wx_pay_check,Toast.LENGTH_SHORT).show();
1069
+                ToastUtils.showShortToast(getApplicationContext(),R.string.wx_pay_check);
1069 1070
                 doCheckOrder(transaction);
1070 1071
                 break;
1071 1072
             case BaseResp.ErrCode.ERR_USER_CANCEL:

+ 56 - 0
app/src/main/java/ai/pai/client/utils/ToastUtils.java

@@ -0,0 +1,56 @@
1
+package ai.pai.client.utils;
2
+
3
+import android.content.Context;
4
+import android.os.Handler;
5
+import android.view.Gravity;
6
+import android.view.LayoutInflater;
7
+import android.view.View;
8
+import android.widget.TextView;
9
+import android.widget.Toast;
10
+
11
+import ai.pai.client.R;
12
+
13
+public class ToastUtils {
14
+
15
+    private static Toast mToast;
16
+
17
+    private static Handler mHandler = new Handler();
18
+    private static Runnable r = new Runnable() {
19
+        public void run() {
20
+            mToast.cancel();
21
+            mToast=null;//toast隐藏后,将其置为null
22
+        }
23
+    };
24
+
25
+    public static void showShortToast(Context context, String message) {
26
+        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
27
+        View view = inflater.inflate(R.layout.custom_toast, null);//自定义布局
28
+        TextView text = (TextView) view.findViewById(R.id.toast_message);//显示的提示文字
29
+        text.setText(message);
30
+        mHandler.removeCallbacks(r);
31
+        if (mToast == null){//只有mToast==null时才重新创建,否则只需更改提示文字
32
+            mToast = new Toast(context);
33
+            mToast.setDuration(Toast.LENGTH_SHORT);
34
+            mToast.setGravity(Gravity.BOTTOM, 0, 150);
35
+            mToast.setView(view);
36
+        }
37
+        mHandler.postDelayed(r, 1000);//延迟1秒隐藏toast
38
+        mToast.show();
39
+    }
40
+
41
+    public static void showShortToast(Context context, int message) {
42
+        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
43
+        View view = inflater.inflate(R.layout.custom_toast, null);//自定义布局
44
+        TextView text = (TextView) view.findViewById(R.id.toast_message);//显示的提示文字
45
+        text.setText(message);
46
+        mHandler.removeCallbacks(r);
47
+        if (mToast == null){//只有mToast==null时才重新创建,否则只需更改提示文字
48
+            mToast = new Toast(context);
49
+            mToast.setDuration(Toast.LENGTH_SHORT);
50
+            mToast.setGravity(Gravity.BOTTOM, 0, 150);
51
+            mToast.setView(view);
52
+        }
53
+        mHandler.postDelayed(r, 1000);//延迟1秒隐藏toast
54
+        mToast.show();
55
+    }
56
+}

+ 20 - 0
app/src/main/res/layout/custom_toast.xml

@@ -0,0 +1,20 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    android:layout_width="wrap_content"
4
+    android:layout_height="35dp"
5
+    android:background="@drawable/round_edittext"
6
+    android:padding="10dp" >
7
+
8
+    <TextView
9
+        android:id="@+id/toast_message"
10
+        android:layout_width="wrap_content"
11
+        android:layout_height="wrap_content"
12
+        android:layout_centerVertical="true"
13
+        android:gravity="center"
14
+        android:maxLines="2"
15
+        android:textColor="@color/dark_grey"
16
+        android:textSize="14sp"
17
+        android:paddingLeft="2dp"
18
+        android:paddingRight="2dp"/>
19
+
20
+</RelativeLayout>