编辑:没有解决方案!至少使用Progress Bar发布代码,也就是获得NullPointer
如何以编程方式更改每个轮换的进度对话框颜色?
码:
if (checkValidation()) {
progressDialog.setTitle(setStringResource(R.string.reg_user));
progressDialog.setMessage(setStringResource(R.string.wait));
progressDialog.setCancelable(false);
progressDialog.show();
registerUser(getName, getEmail, getPassword);
}
还有一个问题。如果我想使用像这样的progressDialog.setMessage(setStringResource(R.string.creating_user));
更改相同进度对话框的标题但是在特定时间段内怎么办?
就像我想在30秒后更改同一进度对话框的标题。
首先,这不是推荐的解决方案,但您可以像下面这样实现它
每秒运行一次计时器并使用以下方法更新它。
ProgressBar progressbar=(ProgressBar) progressDialog.findViewById(android.R.id.progress);
progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C60000"), android.graphics.PorterDuff.Mode.SRC_IN);
编辑
下面写的代码是用kotlin语言编写的
pDialog = ProgressDialog.show(mContext, "Title goes here", "Message goes here")
pDialog?.show()
val progressbar = pDialog!!.findViewById(android.R.id.progress) as ProgressBar
progressbar.indeterminateDrawable.setColorFilter(Color.parseColor("#C60000"), android.graphics.PorterDuff.Mode.SRC_IN)
看输出
在Java中
ProgressDialog progressDialog = ProgressDialog.show(this, "Title goes here", "Message goes here");
progressDialog.show();
ProgressBar progressbar = (ProgressBar) progressDialog.findViewById(android.R.id.progress);
runTimer(progressbar);
在progressDialog初始化之后调用下面的方法。另外,不要忘记在全局变量中引用progressbar。
void runTimer(final ProgressBar progressbar){
final Handler handler = new Handler();
final int delay = 1000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
count++;
switch (count%3){
case 0:
progressbar.getIndeterminateDrawable().setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
break;
case 1:
progressbar.getIndeterminateDrawable().setColorFilter(Color.GREEN, android.graphics.PorterDuff.Mode.SRC_IN);
break;
case 2:
progressbar.getIndeterminateDrawable().setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
break;
}
handler.postDelayed(this, delay);
}
}, delay);
}
您需要全局声明count变量。
int count = 0;
试试这个
progressDialog = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
AnimationDrawable animdrawable = getProgressBarAnimation();
progressDialog.setBackgroundDrawable(animdrawable);
对于动画
private AnimationDrawable getProgressBarAnimation() {
GradientDrawable rainbow1 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW});
GradientDrawable rainbow2 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN});
GradientDrawable rainbow3 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN});
GradientDrawable rainbow4 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE});
GradientDrawable rainbow5 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA});
GradientDrawable rainbow6 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});
GradientDrawable[] gds = new GradientDrawable[]{rainbow1, rainbow2, rainbow3, rainbow4, rainbow5, rainbow6};
AnimationDrawable animation = new AnimationDrawable();
for (GradientDrawable gd : gds) {
animation.addFrame(gd, 100);
}
animation.setOneShot(false);
return animation;
}