有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓在加载另一个acitvity时放置加载消息

其实我的问题很简单,我是安卓新手

如何在用户单击新活动后放置加载消息(文本或进度条)

我有两个活动:主活动和仪表板活动

以下是MainActivity代码:

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

   findViewById(R.id.textViewTest).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            startActivity(new Intent(MainActivity.this, DashboardActivity.class));
        }
    });
  }
}

关于仪表板活动

public class DashboardActivity extends AppCompatActivity
{
    private Toolbar mTopToolbar;
    private AppBarConfiguration mAppBarConfiguration;
    private NavigationView navigationView;
    private int progressStatus = 0;
    private Handler handler = new Handler();
    private ProgressBar progressBar;

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

        // Find the toolbar view inside the activity layout
        Toolbar toolbar = findViewById(R.id.toolbar);
        // Sets the Toolbar to act as the ActionBar for this Activity window.
        // Make sure the toolbar exists in the activity and is not null
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        View hview = navigationView.getHeaderView(0);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
/* end class */
}

请看这些图片:

main活动

enter image description here

当我打开仪表板活动(通过单击仅测试)时,显示空白页面大约4-5秒,如下所示:

enter image description here

在内容完全加载之前

Q:我的期望是,当仪表板活动为空/仍在加载时,如何显示加载文本或进度条

请帮忙

谢谢


共 (1) 个答案

  1. # 1 楼答案

    在加载文本或任何数据时,可以显示progressbar。数据准备好后,只需隐藏进度条并显示文本视图。这里有一个例子

    在DashboardActivity布局中,在preogress栏下方添加一个直接子项,默认情况下可见

       <ProgressBar
            android:id="@+id/progress_bar"
            style="@android:style/Widget.ProgressBar.Large"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="50dp"
            android:visibility="visible"
            android:indeterminate="true"
            android:indeterminateTint="#000000"
            android:indeterminateTintMode="multiply" />
    

    现在通过添加属性隐藏所有文本视图

    android:visibility="invisible"
    

    在DashboardActivity类中,首先当它打开时,进度条开始加载。数据准备好后,将其设置为文本。现在隐藏进度条并显示文本视图

    resultTextView.setVisibility(View.VISIBLE);
    yourProgressBar.setVisibility(View.INVISIBLE);
    

    就这样