Android 使用ProgressDiag和AlertDialog实施注册活动
我想实现一个注册活动,用户在其中插入他/她的信息,然后单击一个按钮将此信息发送到将此信息存储在数据库中的web服务 我将用于连接到web服务的代码放在一个单独的线程中(不是在UI线程中),我想显示一个Android 使用ProgressDiag和AlertDialog实施注册活动,android,android-alertdialog,progressdialog,Android,Android Alertdialog,Progressdialog,我想实现一个注册活动,用户在其中插入他/她的信息,然后单击一个按钮将此信息发送到将此信息存储在数据库中的web服务 我将用于连接到web服务的代码放在一个单独的线程中(不是在UI线程中),我想显示一个progressdialog,直到连接到web服务完成,然后我想显示一个AlertDialog以显示不同的消息,如(此电子邮件用于尝试不同的邮件,或注册成功!) 以下是用户单击“注册”按钮的理由: public void SignupNewUser (View V) { Working =
progressdialog
,直到连接到web服务完成,然后我想显示一个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
是一个保存用户信息的对象,result
是一个整数,指示连接到web服务端后将显示哪个AlertDialog
我的问题是,当我运行上述代码时,不会出现警告对话框消息!
为什么呢?如果你使用了一种新的方法,你会更容易做到这一点。我认为您可能无法获得对话框,因为您试图在启动线程后立即显示它
使用AsyncTask,您可以让服务器连接在单独线程的doInBackground()中运行,然后在onPostExecute()中调用对话框
让我知道这是否有意义!该链接非常清楚如何使用它。:)
编辑:我还想提到,如果您使用AsyncTask,它允许您在onProgressUpdate()方法中轻松设置ProgressDialog 好的,谢谢。。但是,有没有办法在连接结束之前停止代码并保持进度对话框运行???有办法做到这一点,但理想情况下,您不希望像那样挂起线程/应用程序。这也是AsyncTask最适合这样做的另一个原因:它只允许您在后台任务完成后显示对话框。Sweet:)应该不会太糟糕。您几乎可以将run()方法中的代码放入doInBackground()方法中。然后它将返回要在onPostExecute()中显示的对话框的ID@codeMagic我想显示4条消息中的一条(结果将等于连接结束后的以下数字之一“-1,0,1,2”)