【问题标题】:TextView setText AlertDialog delayedTextView setText AlertDialog 延迟
【发布时间】:2014-07-25 20:52:16
【问题描述】:

我正在处理一个在可点击的布局中包含 TextView 的活动。 onClick 方法激活一个警告对话框的打开,该对话框包含 TextView 的可能值(3 个不同的可能值 (0)、(1)、(2))。 (必须使用 AlertDialog 来获取值, Spinner 不起作用)。 int BUSselection 在程序开始时被声明为 -1。

public void showStartingDialog(){
        DialogFragment dialog = new StartingDialogFragment();
        dialog.show(getFragmentManager(), "StartingDialogFragment");
    }
    public void onBUSDialogSelect(DialogFragment dialog, int which)
    {BUSselection = which;}

public void changeStarting(View v) //OnClick method
{
     showStartingDialog();
     TextView basic = (TextView)findViewById(R.id.startingBox);
     if(BUSselection==0)
     {
         basic.setText("0");
     }
     else if (BUSselection ==1)
     {
         basic.setText("1");
     }
     else if (BUSselection ==2)
     {
         basic.setText("2");
     }
     else
     {

     }
}

基本上,这个程序应该在 AlertDialog 中获取列表的选定选项(位置 0 为“0”,位置 1 为“1”,位置 2 为“2”)并将这些值放入 TextView。但是,我的问题是,当我选择位置 1 时,直到我再次单击(再次显示警报对话框)之前,1 才会出现在 TextView 中。我做错了什么?

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    在没有看到更多代码的情况下,我最好的猜测是您可以尝试通过调用 invalidate() 来强制 TextView 更新。

    例如:

    TextView basic = (TextView) v.findViewById(R.id.startingBox);
    .
    .
    basic.setText("1");
    .
    .
    v.invalidate();  // forces view to refresh its components
    

    注意,v 必须引用包含您的 TextView 的视图。

    【讨论】:

      【解决方案2】:

      我认为您的问题是showStartingDialog() 在对话框完成之前返回。 调用后的行应放入onBUSDialogSelect() 方法中。

      类似这样的:

      public void showStartingDialog() {
          DialogFragment dialog = new StartingDialogFragment();
          dialog.show(getFragmentManager(), "StartingDialogFragment");
      }
      
      public void onBUSDialogSelect(DialogFragment dialog, int which) {
          BUSselection = which;
          TextView basic = (TextView)findViewById(R.id.startingBox);
          if(BUSselection==0) {
               basic.setText("0");
          } else if (BUSselection ==1) {
               basic.setText("1");
          } else if (BUSselection ==2) {
               basic.setText("2");
          } else {
      
          }
      }
      
      // OnClick method
      public void changeStarting(View v) {
           showStartingDialog();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多