当ProgressBar在Android中可见时,如何禁用用户交互? [英] How to disable user interaction while ProgressBar is visible in android?
问题描述
我正在使用自定义的ProgressBar.现在,当任务正在进行时,我正在显示进度栏,但是用户仍然可以与视图和控件进行交互. 可见时,如何像ProgressDialog一样在整个视图上禁用用户交互.
I am using a custom ProgressBar. Now while a task is going on, I am showing the progress bar, but user can still interact with the views and controls. How do I disable the user interaction on whole view just like a ProgressDialog does , when it is visible.
我是否需要在主视图上方使用透明视图,并在该视图上显示进度条,并在任务完成后隐藏该视图.
Do I need to use a transparent view on top of main view and show the progress bar on that view and hide that view once a task is completed.
还是只获取我的parentView的ID并将其设置为禁用?但是那样我就无法调暗背景,就像在视图/活动/片段上出现对话框时发生的一样.正确的?
Or just get the id of my parentView and set it disabled ? But then I won't be able to dim the background, just like what happens when a dialog appears on the view/Activity/Fragment. Right?
我只想知道在进度条可见时禁止用户进行任何交互的方法.
I just want to know the way to disallow the user from any interaction while the progressbar is visible.
谢谢
推荐答案
您的问题:当ProgressBar在android中可见时,如何禁用用户交互?
Your question: How to disable the user interaction while ProgressBar is visible in android?
要禁用用户交互,您只需添加以下代码
To disable the user interaction you just need to add the following code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
要恢复用户交互,您只需添加以下代码
To get user interaction back you just need to add the following code
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
以下是示例: 注意:我仅向您提供一个示例,以说明如何禁用或保留用户互动
在xml中添加进度条.类似的事情
Add a progress bar in your xml.Something like this
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:visibility="gone"/>
在MainActivity中,按下按钮时,您将显示进度条并禁用用户交互.
In MainActivity when a button pressed you show the progressbar and disable the user interaction.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProgressBar.setVisibility(View.VISIBLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
}
当用户向后按下时,再次删除进度条,保留用户交互.类似的事情
And when user backPressed you remove the progressbar again retain the user interaction.Something like this
@Override
public void onBackPressed() {
super.onBackPressed();
mProgressBar.setVisibility(View.GONE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
如果要添加禁用和灰显显示功能,则需要在xml布局文件中添加填充父级的线性布局.将其背景设置为#B0000000,并将其visibilty
设置为GONE
.然后以编程方式将其visibility
设置为VISIBLE
.
If you want to add a feature of disable and greyed out display, you need to add in your xml layout file a linear layout that fills the parent. Set its background to #B0000000 and its visibilty
to GONE
. Then programmatically set its visibility
to VISIBLE
.
希望获得帮助!
这篇关于当ProgressBar在Android中可见时,如何禁用用户交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!