简体   繁体   English

Android对话框,更改小部件可见性

[英]Android Dialog box, change widget visibility

This is my code, please suggest why the Progressbar do not go invisible to show the TextView. 这是我的代码,请提出为什么进度栏不变得不可见以显示TextView的原因。 I am switching the visibility initially and on Post execution yet I get only the progress bar showing but not the textview showing. 我最初切换可见性并且在执行后切换,但是我只显示进度条,而不显示textview。 Any suggestion? 有什么建议吗?

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();
}

} }

This is my alert box layout it is a custom dialog box, so I can get a progress bar. 这是我的警报框布局,它是一个自定义对话框,因此我可以获得进度栏。

<?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>

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

progressBar.setVisibility(View.GONE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM