应用程序注册按钮不起作用

时间:2014-12-26 07:47:49

标签: 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 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");
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

只需在onCreate()方法

上添加此内容即可
    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();
                    }
                }
            });

每次你遇到signupbutton这个方法(听众)都会调用。

在您定义xml

signupbutton文件中添加此行
android:onClick="sign"

但对我来说最好的方法是添加clickListener,因为它从java类中清楚地看到了点击按钮时调用的内容。

答案 1 :(得分:0)

您是否在布局目录下的signup.xml中提到了android:onClick =“sign”?请确认。

答案 2 :(得分:0)

我认为您的代码是核心。 添加Log to sign功能:

public void sign(View v) {
    Log.e("tag","sign function called");
    try {
        // CALL validate method
        validate();
    } catch (Exception ex) {
        String error = ex.getMessage();
    }

}

如果是corect,则可以显示日志:&#34;签名函数称为&#34;