如何更新活动对话框片段中的textview?

时间:2014-03-17 17:27:43

标签: android dialog

这可能是一个愚蠢的问题,但我没有找到一个从我的Android应用程序中的活动更新对话碎片的textview的好方法。 我想做的是每秒用计数器值更新textview,一旦时间过去,Runnable就会关闭对话框片段。 一旦时间过去,对话框就会关闭,没问题,但是我无法更新我想要的文本视图。

这是我对话框的代码:

public class AlertDialog extends DialogFragment {

private String message = null;
private String title = null;
private ImageView imgV = null;
private TextView msgTv = null;
private TextView counterTv = null;
private  Button okBtn = null;
private int imageId = 0;
public static int AUTOMATIC_CLOSE = 100001;

private AlertDialogListener mDialogListener;

public void setImage(int i){
    imageId = i;
}

public void setContent(String ttl, String msg){
    message = msg;
    title = ttl;
}

public boolean hasContent(){
    return message != null && title != null;
}

public AlertDialog(){

}

public void performClick(){
    okBtn.performClick();
}

public void updateField(String text){
    counterTv.setText(text);
}

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

    View v = inflater.inflate(R.layout.dialog, container);
    msgTv = (TextView)v.findViewById(R.id.textDialog);
    imgV = (ImageView)v.findViewById(R.id.imageDialog);
    counterTv = (TextView)v.findViewById(R.id.timeCounterDialog);

    if(imageId != 0)
        imgV.setImageResource(imageId);
    else
        imgV.setImageResource(R.drawable.error_icon);

    if(hasContent()){
        msgTv.setText(message);
        getDialog().setTitle(title);
    }
    else{
        getDialog().setTitle("ERROR");
        msgTv.setText("An unexcepted error occured");
    }

    okBtn = (Button)v.findViewById(R.id.validateButton);

    okBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mDialogListener != null){
                mDialogListener.onFinishedDialog();
            }
        }
    });

    return v;
}

@Override
public void onStart() {
    mDialogListener.onStartedDialog();
    super.onStart();
}

@Override
public void onAttach(Activity activity) {
    mDialogListener = (AlertDialogListener) activity;
    super.onAttach(activity);
}

@Override
public void onDetach() {
    mDialogListener = null;
    super.onDetach();
}

public interface AlertDialogListener{
    void onStartedDialog();
    void onFinishedDialog();
}

}

这就是我发布它的方式:

class myActivity extends Activity implements AlertDialogListener{

    protected void onCreate(Bundle savedInstanceState){
        """some init stuff"""
        button.setOnClickListener(new View.OnClickListener() {
            showAlertDialog();
        }
    }

@Override
public void onStartedDialog() {
    AutoCloseRunnable mAutoClose = new AutoCloseRunnable();
    mHandler.postDelayed(mAutoClose, 1000);

}

@Override
public void onFinishedDialog() {
    this.finish();
}

private void showAlertDialog(){
    FragmentManager fm = getFragmentManager();
    mAlertDialog = new AlertDialog();
    mAlertDialog.setContent("No Connection available", "Please enable your internet connection.");
    mAlertDialog.setImage(R.drawable.error_icon);
    mAlertDialog.show(fm, "fragment_alert");

}

private void updateAlertDialog(String text){
    mAlertDialog.updateField(text);
}

private void autoCloseAlertDialog(){
    mAlertDialog.performClick();
}


public class AutoCloseRunnable implements Runnable{

    @Override
    public void run() {
        int closeCpt = 10;

        while(closeCpt >= 0){
            try {
                Thread.sleep(1000);
                updateAlertDialog("Will close automatically in " + closeCpt + " seconds.");
                closeCpt--;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        autoCloseAlertDialog();
    }
}

}

有谁知道如何继续?

1 个答案:

答案 0 :(得分:0)

我解决了它,只需使用asynctask就可以更轻松地处理这样的UI更新。