[英]Edittext nor ProgressDialog doesn't appear
我遇到一個問題,我的進度對話框不顯示或不執行任何操作。 我正在制作一個可以轉換值的貨幣轉換器。 這是我的代碼:
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
while (edittextdollars.equals("")) {
final ProgressDialog myPd_ring=ProgressDialog.show(getActivity(), "", "Loading...", true);
myPd_ring.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try
{
convertvalues("USD", "EUR");
}catch(Exception e){
}
myPd_ring.dismiss();
}
}).start();
}
}
沒有進度對話框,如所示:
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
convertvalues("USD", "EUR");
}
它有效,但是在返回值之前,按下了“計算”按鈕幾秒鍾。 我想要一個progressdialog顯示該值正在加載。 我已經搜索過Google,但沒有任何效果。 任何有關此問題的幫助表示贊賞。
PS:我希望您發布代碼,而不是只說一個沒有任何代碼的解決方案。 另外,請發布任何您認為對我有幫助的網站(例如教程)。 再次感謝。
編輯:
當我添加以下代碼時:
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
myPd_ring = ProgressDialog.show(getActivity(), "", "Loading...", true, true);
new Thread(new Runnable() {
@Override
public void run() {
try {
convertvalues("USD", "EUR");
} catch(Exception e) {
}
}
}).start();
}
@Override
public void afterTextChanged(Editable arg0) {
myPd_ring.dismiss();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
我已經實現了TextWatcher,但是它不起作用。 progressdialog會一直保留在那里,並且值不會改變。 任何幫助將不勝感激。
您所做的不正確。
while (edittextdollars.equals("")) { // this while loop is not the way to wait for calculation finish...inside this while loop you are spwaning a thread which is not correct
final ProgressDialog myPd_ring=ProgressDialog.show(getActivity(), "", "Loading...", true); //here you are showing the dialog
myPd_ring.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
convertvalues("USD", "EUR");
} catch(Exception e) {
}
myPd_ring.dismiss(); // the run method can be called within few milliseconds of starting the thread..so in essence immediately you are removing your dialog
}
}).start(); // just after showing the dialog you are starting the thread
}
我建議進行以下更改(我假設您的方法convertvalues
是更改edittexteuros
文本的edittexteuros
):
1.在活動課程中聲明myPd_ring
ProgressDialog myPd_ring = null;
2.讓您的活動實現TextWatcher(添加未實現的方法)
public class YourActivity extends Activity implements TextWatcher
3.將TextWatcher與您的EditText edittexteuros
一起添加。
edittexteuros = (EditText) findViewById(R.id.youreditTextId);
edittexteuros.addTextChangedListener(YourActivity.this);
4.計算部分並顯示對話框
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
myPd_ring = ProgressDialog.show(getActivity(), "", "Loading...", true, true);
new Thread(new Runnable() {
@Override
public void run() {
try {
convertvalues("USD", "EUR");
} catch(Exception e) {
}
}
}).start();
}
5.從afterTextChanged
解散Dialog
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
myPd_ring.dismiss();
}
編輯:
您確定current
始終是有效值嗎? 作為預防措施,您需要在異常情況下顯示一些錯誤文本(您始終需要設置一些文本,否則對話框將永遠不會關閉)。 請調試並檢查您是否始終從網絡獲得正確的響應。 我提供給您的代碼可以在我的測試設置中完美運行,因此您的問題可能現在出在其他地方。 您將需要調試並找出答案。
YahooCurrencyConverter ycc = new YahooCurrencyConverter();
try {
current = ycc.convert(convertfrom, convertto);
edittexteuros.setText(df.format(val*current));
return "passed";
} catch (Exception e) {
edittexteuros.setText("some error message");
return "passed";
}
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
showprogress();
convertvalues("USD", "EUR");
dissmissprogress();
}
showprogress(){
dialog = new ProgressDialog(youractivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show()
}
dissmissprogress(){
dialog.dismiss();
}
希望這是您想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.