简体   繁体   English

不会出现Edittext或ProgressDialog

[英]Edittext nor ProgressDialog doesn't appear

I am encountering a problem where my progressdialog doesn't show or do anything. 我遇到一个问题,我的进度对话框不显示或不执行任何操作。 I am making a currency converter that converts values. 我正在制作一个可以转换值的货币转换器。 Here is my code: 这是我的代码:

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();
            }
        }

Without the progressdialog, like shows: 没有进度对话框,如所示:

    if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {

                convertvalues("USD", "EUR");

}

It works, but the "Calculate" button is pressed down for a few seconds before the value returns. 它有效,但是在返回值之前,按下了“计算”按钮几秒钟。 I want a progressdialog to show that the value is loading. 我想要一个progressdialog显示该值正在加载。 I have searched Google, but nothing worked. 我已经搜索过Google,但没有任何效果。 Any help regarding this problem is appreciated. 任何有关此问题的帮助表示赞赏。

PS I would prefer you post code instead of just saying a solution without any code. PS:我希望您发布代码,而不是只说一个没有任何代码的解决方案。 Also, please post any websites that you would think is helpful for me (like tutorials). 另外,请发布任何您认为对我有帮助的网站(例如教程)。 Thanks once again. 再次感谢。

EDIT: 编辑:

When I added this code: 当我添加以下代码时:

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

}

I have implemented TextWatcher, but it doesn't work. 我已经实现了TextWatcher,但是它不起作用。 The progressdialog stays there forever and the value doesn't change. progressdialog会一直保留在那里,并且值不会改变。 Any help would be greatly appreciated. 任何帮助将不胜感激。

What you have done is not correct. 您所做的不正确。

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
}

I would suggest the following changes (I am assuming that your method convertvalues is the one which changes the text of edittexteuros ): 我建议进行以下更改(我假设您的方法convertvalues是更改edittexteuros文本的edittexteuros ):

1. Declare myPd_ring in your activity class 1.在活动课程中声明myPd_ring

ProgressDialog myPd_ring = null; 

2. Let your activity implement TextWatcher (add the unimplemented methods) 2.让您的活动实现TextWatcher(添加未实现的方法)

public class YourActivity extends Activity implements TextWatcher

3. Add TextWatcher with your EditText edittexteuros . 3.将TextWatcher与您的EditText edittexteuros一起添加。

edittexteuros = (EditText) findViewById(R.id.youreditTextId);
edittexteuros.addTextChangedListener(YourActivity.this);

4. The calculation Part and showing the Dialog 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. The dismissal of Dialog from afterTextChanged 5.从afterTextChanged解散Dialog

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
    myPd_ring.dismiss();
}

EDIT: 编辑:

Are you sure that current is always a valid value? 您确定current始终是有效值吗? As a precaution you need to show some error text in case of exception (you always need to set some text else the dialog will never close). 作为预防措施,您需要在异常情况下显示一些错误文本(您始终需要设置一些文本,否则对话框将永远不会关闭)。 Please debug and check if you are always getting a proper response from the network or not. 请调试并检查您是否始终从网络获得正确的响应。 The code which I have given to you works perfectly in my test setup, so your problem might be lying somewhere else now. 我提供给您的代码可以在我的测试设置中完美运行,因此您的问题可能现在出在其他地方。 You will need to debug and find that out. 您将需要调试并找出答案。

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();
}

Hope this is what you are looking for. 希望这是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM