如何在其他Activity或Fragment中调用Fragment或Activity的方法

时间:2015-07-18 12:20:32

标签: android

公共类CustomToastEx扩展Fragment {

View layout;
public TextView text;
Toast toast;
public LinearLayout ll;
public ImageView toastImg;
String colors="#42ED39";
Context context;

@SuppressLint("NewApi")
public CustomToastEx(String text, Drawable toastImg, int gravity, int xOffset, int yOffset) {
    super();
    this.text.setText(text);
    this.toastImg.setBackground(toastImg);
    toast.setGravity(gravity, xOffset, yOffset);

}
public CustomToastEx(Activity activity){
    context=activity.getApplicationContext();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ll=(LinearLayout)layout.findViewById(R.id.toast_layout_root);
    toastImg    =(ImageView) layout.findViewById(R.id.imgView);
    text = (TextView) layout.findViewById(R.id.text);
    text.setBackgroundColor(0xFFFF0000);
    text.setTextColor(getResources().getColor(R.color.darkgreen));
    text.setText("This is a custom toast\nHello toast");
    toast = new Toast(context);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

public void setText(String text){
    this.text.setText(text);
}
public void setDuration(int time){
    toast.setDuration(time);
}
public void setGravity(int gravity, int xOffset, int yOffset){
    toast.setGravity(gravity, xOffset, yOffset);
}
public void setTextColor(int color){
    text.setTextColor(color);
}
public void setBackgroundColor(int color){
    ll.setBackgroundColor(color);
}
@SuppressLint("NewApi")
public void setToastImage(Drawable background){
    toastImg.setBackground(background);
}
public void setClockwise(){
    Animation animation = AnimationUtils.loadAnimation(context, R.anim.clockwise);
    toastImg.startAnimation(animation);
 }
public void setBlink(){
    Animation animation1 = AnimationUtils.loadAnimation(context, R.anim.blink);
    toastImg.startAnimation(animation1);
 }
public void show(){
    toast.show();
}

}

调用片段方法的活动代码:

公共类MainActivity扩展了Activity {

View layout
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CustomToastEx toast=new CustomToastEx(this);
    toast.setText("Hello");
    toast.show();
            }

我想设置toast的属性,直到android.Doing才能实现,所以给出nullPointerException

0 个答案:

没有答案