Android 进度条出现错误
我创建了一个 Android 应用程序。在该程序中,我有两种布局。当我单击第一个布局中的按钮时,它将显示带有进度条的第二个布局。我的问题是单击按钮后进度条被放置在第一个布局中。我该如何解决这个问题?
这是我的代码:
// Get the increment value from the text box
myTitleText = (EditText) findViewById(R.id.editText1);
s = myTitleText.getText().toString();
con=myTitleText.length();
//Convert the textvalue to a integer value
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
// Set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// Reset the bar to the default value of 0
dialog.setProgress(0);
// Get the maximum value
int maximum=jstr.length();
// Set the maximum value
dialog.setMax(maximum);
// Display the progress bar
dialog.show();
// Create a thread for updating the progress bar
Thread background = new Thread (new Runnable() {
public void run() {
try {
// Enter the code to be run while displaying the progress bar.
//
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while (dialog.getProgress()<= dialog.getMax()) {
// Wait 500 ms between each update.
Thread.sleep(500);
// Activate the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
}
catch (java.lang.InterruptedException e) {
// If something fails, do something smart
}
}
});
// Start the background thread
background.start();
// Handler for the background updating
progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(con);
}
};
I have created an Android application. In that program, I have two layouts. When I click the button in the first layout, it will display the second layout with the progress bar. My problem is the progress bar is placed in the first layout after I click the button. How do I fix this problem?
Here is my code:
// Get the increment value from the text box
myTitleText = (EditText) findViewById(R.id.editText1);
s = myTitleText.getText().toString();
con=myTitleText.length();
//Convert the textvalue to a integer value
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
// Set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// Reset the bar to the default value of 0
dialog.setProgress(0);
// Get the maximum value
int maximum=jstr.length();
// Set the maximum value
dialog.setMax(maximum);
// Display the progress bar
dialog.show();
// Create a thread for updating the progress bar
Thread background = new Thread (new Runnable() {
public void run() {
try {
// Enter the code to be run while displaying the progress bar.
//
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while (dialog.getProgress()<= dialog.getMax()) {
// Wait 500 ms between each update.
Thread.sleep(500);
// Activate the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
}
catch (java.lang.InterruptedException e) {
// If something fails, do something smart
}
}
});
// Start the background thread
background.start();
// Handler for the background updating
progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(con);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望在单击按钮后进度条出现在第一个布局本身上,请将进度条代码的 XML 部分放置在第一个布局中。
If you want the progress bar to appear on the first layout itself after the button click, then place the XML part of code for progress bar in that first layout.