Android:使用旋转轮进度对话框无法正常工作

时间:2013-04-02 13:14:47

标签: android dialog

我正在设计一个登录屏幕,并希望在点击“登录”按钮时显示带有“请等待...”文本的旋转轮。如果用户名和密码匹配,则应显示下一个屏幕,否则应显示错误消息。我的问题是,当按下按钮时,进程对话框即将出现,但是关闭对话框后,下一个屏幕或错误消息不会显示。我有以下代码。请帮助我在哪里做错了。

  package com.example.first_db_app;

  import java.util.List;

  import android.os.Bundle;
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;

 public class TestDbActivity extends Activity
 {




@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_db);


        DatabaseHandler db=new DatabaseHandler(this);


        //CRUD operations

        //inserting the records
        /*Log.d("Insert: ", "Inserting ..");
        db.addUser(new User("Ashwin","11111"));
        db.addUser(new User("Ravi","22222"));
        db.addUser(new User("Gopal","33333"));
        db.addUser(new User("Satish","44444"));*/


         // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<User> user = db.getAllContacts();       

        for (User us : user) {
            String log = "Name: " + us.getUname()  + " ,Password " + us.getUpwd();
                // Writing Contacts to log
        Log.d("Name: ", log);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_db, menu);
        return true;
    }

    public void login(View v)
    {
        final  EditText edtuname=(EditText) findViewById(R.id.editTextUserNameToLogin);
        final  EditText edtpwd=(EditText) findViewById(R.id.editTextPasswordToLogin);
          Button btnlogin=(Button) findViewById(R.id.buttonSignIn);
         final DatabaseHandler db=new DatabaseHandler(this);

          btnlogin.setOnClickListener(new View.OnClickListener()
          {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                final ProgressDialog mypd=ProgressDialog.show(TestDbActivity.this, "", "Loading..",true);
                new Thread (new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {

                            String uname= edtuname.getText().toString();
                            String pwd=edtpwd.getText().toString();
                            String stored_pwd=db.getUser(uname);
                            Thread.sleep(5000);

                            if(pwd.equals(stored_pwd))
                            {



                                //Toast.makeText(TestDbActivity.this,"successful", Toast.LENGTH_LONG).show();
                                mypd.dismiss();
                                setContentView(R.layout.activity_next);
                            }
                            else
                            {
                                mypd.dismiss();
                                Toast.makeText(TestDbActivity.this, "failed", Toast.LENGTH_LONG).show();

                            }
                        }
                        catch (Exception e)
                        {

                        }


                    }

                }).start();





            }
        } );

    }


}


Thanks in advance.

2 个答案:

答案 0 :(得分:2)

您无法从非ui线程中显示Toast。而是尝试显示如下:

TestDbActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(TestDbActivity.this, "failed", Toast.LENGTH_LONG).show();
    }
});

更优选的是,我建议您在此处使用AsyncTask

答案 1 :(得分:1)

我认为这里最好的选择是使用AsyncTask,因为它在后台线程上运行,并且在执行此操作时您可以显示进度对话框。这是一个简单的例子,你如何实现这个目标:

private ProgressDialog progressDialog;

private class LoginUser extends AsyncTask<String, Void, Boolean>{

    private String username;
    private String password;

    @Override
    protected Boolean doInBackground(String... params) {

        if(params != null && params.length >= 1){
            username = params[0];
            password = params[1];

            // if everything is ok return true
            return true;

        }

        return false;
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            progressDialog = new ProgressDialog(Test.this);
        } else {
            progressDialog = new ProgressDialog(Test.this,
                    AlertDialog.THEME_HOLO_LIGHT);
        }
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Logging in...");
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean result){
        super.onPostExecute(result);
        progressDialog.dismiss();
        if(result){
            // open new activity
        } else {
            // show your error dialog.
        }

    }

}

你可以通过你的按钮onClick来调用它:

btnlogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
          new LoginUser().execute("MyUsername", "MyPassword");
         }
});