0

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

  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.
4

2 回答 2

2

您不能显示Toast来自非 ui 线程的 a 。而是尝试如下显示:

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

更可取的是,我建议您在这里使用AsyncTask

于 2013-04-02T13:20:23.947 回答
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");
         }
});
于 2013-04-02T13:25:17.090 回答