Android 应用程序注册按钮不工作
这是我的java文件,我在我的xml文件中使用了onclick函数,这与使用登录按钮的方法相同,但不适用于注册。请帮助我解决这个问题Android 应用程序注册按钮不工作,android,android-layout,android-intent,android-activity,Android,Android Layout,Android Intent,Android Activity,这是我的java文件,我在我的xml文件中使用了onclick函数,这与使用登录按钮的方法相同,但不适用于注册。请帮助我解决这个问题 public class signup extends ActionBarActivity { EditText username, pass, cpass, mail, phn; String uname, password, confirmpass, email, phone; @Override protected
public class signup extends ActionBarActivity {
EditText username, pass, cpass, mail, phn;
String uname, password, confirmpass, email, phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
username = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
cpass = (EditText) findViewById(R.id.comfirmpass);
mail = (EditText) findViewById(R.id.email);
phn = (EditText) findViewById(R.id.phone);
Button signupbutton = (Button) findViewById(R.id.signupbutton);
}
//When the send button is clicked
public void sign(View v) {
try {
// CALL validate method
validate();
} catch (Exception ex) {
String error = ex.getMessage();
}
}
//Method to get list value pair and form the query
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
//Data intialization and Validation
public void validate() {
// Get user defined values
uname = username.getText().toString();
email = mail.getText().toString();
password = pass.getText().toString();
confirmpass = cpass.getText().toString();
phone = phn.getText().toString();
if (password.equals(confirmpass)) {
post();
} else {
Toast.makeText(getBaseContext(), "Password mismatch", Toast.LENGTH_SHORT).show();
//Reset password fields
pass.setText("");
cpass.setText("");
}
}
public void error(boolean flag, String etext) {
if (flag == true) {
Toast.makeText(getBaseContext(), etext, Toast.LENGTH_SHORT).show();
//Code to handle failure
} else {
Toast.makeText(getBaseContext(), etext, Toast.LENGTH_SHORT).show();
setContentView(R.layout.login);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Method to post data to webservice
public void post() {
try
{
// Calling async task to get json
new DownloadOperation().execute();
}
catch (Exception e) {
e.printStackTrace();
}
}
private class DownloadOperation extends AsyncTask<Void, Void, String> {
ProgressDialog dialog;
String uname = "";
String email = "";
String password = "";
String confirmpass = "";
String phone = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
// Get user defined values
uname = username.getText().toString();
email = mail.getText().toString();
password = pass.getText().toString();
confirmpass = cpass.getText().toString();
phone = phn.getText().toString();
//Initiate ProgressBar
dialog = ProgressDialog.show(signup.this, "Please Wait", "Registering ...");
}
@Override
protected String doInBackground(Void... params) {
String response = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://rgbpallete.in/led/api/signup");
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("uname", uname));
nameValuePairs.add(new BasicNameValuePair("pass", password));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("phone", phone));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpResponse = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
return response;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String jsonStr) {
super.onPostExecute(jsonStr);
dialog.dismiss();
Log.d("tag", "Result:\n" + jsonStr);
if (jsonStr != null) {
try{
JSONObject jsonObj = new JSONObject(jsonStr);
String message = jsonObj.getString("message");
boolean error = jsonObj.getBoolean("error");
error(error,message);
}
catch (JSONException e) {
e.printStackTrace();
}
}
else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
}
}
公共类注册扩展了ActionBarActivity{
EditText用户名、密码、注册会计师、邮件、phn;
字符串uname、密码、confirmpass、电子邮件、电话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
用户名=(EditText)findViewById(R.id.username);
pass=(EditText)findViewById(R.id.password);
cpass=(EditText)findViewById(R.id.comfirmpass);
mail=(EditText)findViewById(R.id.email);
phn=(EditText)findViewById(R.id.phone);
按钮signupbutton=(按钮)findViewById(R.id.signupbutton);
}
//单击“发送”按钮时
公共无效标志(视图五){
试一试{
//调用验证方法
验证();
}捕获(例外情况除外){
字符串错误=ex.getMessage();
}
}
//方法获取列表值对并形成查询
私有字符串getQuery(列表参数)引发UnsupportedEncodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
for(NameValuePair对:params){
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(pair.getName(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(pair.getValue(),“UTF-8”);
}
返回result.toString();
}
//数据初始化和验证
public void validate(){
//获取用户定义的值
uname=username.getText().toString();
email=mail.getText().toString();
password=pass.getText().toString();
confirmpass=cpass.getText().toString();
phone=phn.getText().toString();
if(密码等于(确认通过)){
post();
}否则{
Toast.makeText(getBaseContext(),“密码不匹配”,Toast.LENGTH_SHORT.show();
//重置密码字段
pass.setText(“”);
cpass.setText(“”);
}
}
公共无效错误(布尔标志,字符串etext){
如果(标志==真){
Toast.makeText(getBaseContext(),etext,Toast.LENGTH_SHORT).show();
//处理失败的代码
}否则{
Toast.makeText(getBaseContext(),etext,Toast.LENGTH_SHORT).show();
setContentView(R.layout.login);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//方法将数据发布到webservice
公共空缺职位(){
尝试
{
//调用异步任务以获取json
新建下载操作().execute();
}
捕获(例外e){
e、 printStackTrace();
}
}
私有类下载操作扩展了异步任务{
进程对话;
字符串uname=“”;
字符串email=“”;
字符串密码=”;
字符串confirmpass=“”;
字符串phone=“”;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//获取用户定义的值
uname=username.getText().toString();
email=mail.getText().toString();
password=pass.getText().toString();
confirmpass=cpass.getText().toString();
phone=phn.getText().toString();
//启动ProgressBar
dialog=ProgressDialog.show(注册,这是“请稍候”,“注册…”);
}
@凌驾
受保护字符串doInBackground(无效…参数){
字符串响应=”;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://rgbpallete.in/led/api/signup");
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
试一试{
List nameValuePairs=新的ArrayList(4);
添加(新的BasicNameValuePair(“uname”,uname));
添加(新的BasicNameValuePair(“pass”,password));
添加(新的BasicNameValuePair(“email”,email));
添加(新的BasicNameValuePair(“phone”,phone));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpResponse=httpclient.execute(httppost);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
返回响应;
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串jsonStr){
super.onPostExecute(jsonStr);
dialog.dismise();
Log.d(“标记”,“结果:\n”+jsonStr);
if(jsonStr!=null){
试一试{
signupbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Button is clicked", 3000).show();
///what ever you want to do on signupbtn comes here
//add this try block if you need same functionality as `sign() method`
try {
// CALL validate method
validate();
} catch (Exception ex) {
String error = ex.getMessage();
}
}
});
android:onClick="sign"
public void sign(View v) {
Log.e("tag","sign function called");
try {
// CALL validate method
validate();
} catch (Exception ex) {
String error = ex.getMessage();
}
}