使用Progressdiaog和AlertDialog实现注册actitvty

时间:2013-10-07 14:55:36

标签: android alertdialog progressdialog

我想实现注册活动,用户可以在其中插入他/她的信息,然后单击按钮将此信息发送到将此信息存储在数据库中的Web服务。

我将用于连接到Web服务的代码放在一个单独的线程中(不在UI线程中),我希望显示progressdialog,直到与Web服务的连接完成,然后我想显示一个{{ 1}}显示不同的消息(如使用此电子邮件尝试不同的消息,或注册成功!)

这是用户点击注册按钮时的借口:

AlertDialog

此处,public void SignupNewUser (View V) { Working = ProgressDialog.show(this, "Working..", "Connecting To Server"); Runnable work = new Runnable() { @Override public void run() { Edit_Text_FName = (EditText) findViewById(R.id.Edit_Text_Fname_Signup); Edit_Text_LName = (EditText) findViewById(R.id.Edit_Text_Lname_Signup); Edit_Text_Password = (EditText) findViewById(R.id.Edit_Text_Password_Signup); Edit_Text_Email = (EditText) findViewById(R.id.Edit_Text_Email_Signup); S1 = (Spinner) findViewById(R.id.Spinner_Signup); SignupPerson SUPerson = new SignupPerson(); SUPerson.F_Name = Edit_Text_FName.getText().toString().trim(); SUPerson.L_Name = Edit_Text_LName.getText().toString().trim(); SUPerson.E_Mail = Edit_Text_Email.getText().toString().trim(); SUPerson.PassW = Edit_Text_Password.getText().toString().trim(); SUPerson.Gen = Choosen_Gender; SUPerson.Cou_Id = S1.getSelectedItemPosition(); METHOD = "signup"; SoapObject Request = new SoapObject(NAMESPACE, METHOD); PropertyInfo P = new PropertyInfo(); P.setName("SUPerson"); P.setValue(SUPerson); P.setType(SUPerson.getClass()); Request.addProperty(P); SoapSerializationEnvelope envolope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11); envolope.dotNet = true; envolope.setOutputSoapObject(Request); envolope.addMapping(NAMESPACE, "SignupPerson", new SignupPerson().getClass()); HttpTransportSE ahttp = new HttpTransportSE(URL); SoapPrimitive Res = null; try { ahttp.call(NAMESPACE+METHOD, envolope); Res = (SoapPrimitive) envolope.getResponse(); } catch (Exception ex) { //ex.printStackTrace(); result = -1; } if (result != -1) { result = Integer.parseInt(Res.toString()); } Working.dismiss(); } }; Thread SS = new Thread(work); SS.start(); switch (result) { case -1: showDialog(-1); break; case 0: showDialog(0); break; case 1: showDialog(1); break; case 2: showDialog(2); break; default:break; } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case -1: return new AlertDialog.Builder(this) .setTitle("error!") .setMessage("error connecting to the server. please try again") .setIcon(R.drawable.ic_error) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .create(); case 0: return new AlertDialog.Builder(this) .setTitle("error!") .setMessage("You have entered an Exists Email, Please try another one") .setIcon(R.drawable.ic_error) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }).create(); case 1: return new AlertDialog.Builder(this) .setTitle("error!") .setMessage("Server Error, Please Try Again Later") .setIcon(R.drawable.ic_error) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .create(); case 2: return new AlertDialog.Builder(this) .setTitle("Registration successfully!") .setMessage("Click OK to Sign in and Start Usign Hello!!") .setIcon(R.drawable.ic_success) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Intent i = new Intent(SignupActivity.this ,MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } }) .create(); } return null; } 是一个保存用户信息的对象,SUPerson是一个整数,表示连接到Web服务端后将显示哪个result

我的问题是,当我运行上面的代码时,会出现No Alert Dialog消息! 为什么?

1 个答案:

答案 0 :(得分:0)

如果您使用AsyncTask,那么您可以更轻松地完成此操作。我想你可能没有得到你的对话,因为你试图在启动线程后立即显示它。

使用AsyncTask,您可以在单独的线程上的doInBackground()中运行服务器连接,然后可以在onPostExecute()中调用对话框。

如果有意义,请告诉我!关于如何使用它的链接非常明确。 :)

编辑:我还想提一下,如果你使用AsyncTask,它允许你在onProgressUpdate()方法中轻松设置ProgressDialog。