【问题标题】:Android Dialog box, change widget visibilityAndroid 对话框,更改小部件可见性
【发布时间】:2015-10-07 04:24:09
【问题描述】:

这是我的代码,请说明为什么 Progressbar 不隐藏以显示 TextView。我最初是在切换可见性并在执行后切换,但我只显示进度条,但没有显示文本视图。有什么建议吗?

public class myClass extends BaseActivity implements View.OnClickListener{

Button mSaveButton;
final Context context = this;
Dialog dialog;
TextView text;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    getSupportActionBar().hide();



    mSaveButton = (Button)findViewById(R.id.saveBtn);
    findViewById(R.id.txt_left_menu).setOnClickListener(this);
    findViewById(R.id.saveBtn).setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.saveBtn){

        dialog = new Dialog(context);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.alert_layout);

        text = (TextView) dialog.findViewById(R.id.text);
        progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);

        progressBar.setVisibility(View.VISIBLE);
        text.setVisibility(View.INVISIBLE);
        dialog.show();

        new myTask().execute();
    }
}


class myTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {

        // some preExecute statements
    }

    @Override
    protected Void doInBackground(Void... params) {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // some execution statements
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        progressBar.setVisibility(View.INVISIBLE);
        text.setVisibility(View.VISIBLE);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        dialog.dismiss();
        finish();
    }


@Override
public void onBackPressed() {
    finish();
}

}

这是我的警报框布局,它是一个自定义对话框,所以我可以得到一个进度条。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Payment Received"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:id="@+id/progressBar"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

</RelativeLayout>

【问题讨论】:

    标签: java android android-asynctask dialog


    【解决方案1】:

    您必须在onPostExecute 中将ProgressBar 的可见性更改为GONE

    progressBar.setVisibility(View.GONE);
    

    【讨论】:

    • 我刚做了,还是什么都没有,进度条还是可见的
    • 试试progressBar.dismiss();那么
    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 2017-08-04
    • 2017-07-25
    • 2012-11-04
    相关资源
    最近更新 更多