0

我正在尝试使用我的自定义ToastAsyncTask不幸的是我不能直接使用它,所以我在这里搜索。我读了一些关于创建一个抽象类的帖子,我可以在其中放置自定义吐司的方法。但是我在onPostexecute()我的AsyncTask.

ToastClass.java

public class ToastClass extends Activity
{
     public void ViewToast(String toast_msg){

          Typeface tfR= Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF");
          LayoutInflater inflater = getLayoutInflater();
          View layouttoast = inflater.inflate(R.layout.toast_bg, (ViewGroup)findViewById(R.id.toastAttribute));
          TextView msg = ((TextView) layouttoast.findViewById(R.id.txt_toast));
          msg.setTypeface(tfR);
          msg.setText(toast_msg);
          msg.setTextSize(TypedValue.COMPLEX_UNIT_PX,16);
          Toast mytoast = new Toast(getBaseContext());
          mytoast.setView(layouttoast);
          mytoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
          mytoast.setDuration(Toast.LENGTH_SHORT);
          mytoast.show();

     }
}

ToastActivity.java

public abstract class ToastActivity extends Activity{

     protected void ViewToast(String toast_msg){

         Typeface tfR= Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF");
         LayoutInflater inflater = getLayoutInflater();
         View layouttoast = inflater.inflate(R.layout.toast_bg, (ViewGroup)findViewById(R.id.toastAttribute));
         TextView msg = ((TextView) layouttoast.findViewById(R.id.txt_toast));
         msg.setTypeface(tfR);
         msg.setText(toast_msg);
         msg.setTextSize(TypedValue.COMPLEX_UNIT_PX,16);
         Toast mytoast = new Toast(getBaseContext());
         mytoast.setView(layouttoast);
         mytoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
         mytoast.setDuration(Toast.LENGTH_SHORT);
         mytoast.show();

     }
}

我这样称呼

ToastClass toast = new ToastClass();
toast.ViewToast(msg);

你能教我正确的做法吗?

4

0 回答 0