简体   繁体   English

android studio 按钮不起作用

[英]android studio button not working

I created a android studio button for my app and when I click on the register button it doesn't work .我为我的应用程序创建了一个 android studio 按钮,当我点击注册按钮时它不起作用。 I don't get any errors it just doesn't work .我没有收到任何错误它只是不起作用。 When the user clicks the register button I want to go to the login activity .当用户单击注册按钮时,我想转到登录活动。

RegisterActivity.java :注册活动.java :

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

 public class RegisterActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    final EditText etUsername = (EditText) findViewById(R.id.etUsername);
    final EditText etPassword = (EditText) findViewById(R.id.etPassword);
    final Button bRegister = (Button) findViewById(R.id.bRegister);

    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String username = etUsername.getText().toString();
            final String password = etPassword.getText().toString();

            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");
                        if (success) {
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest( username, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }
    });
}

}

activity_register.xml :活动注册.xml :

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Register"
    android:id="@+id/bRegister"
    android:layout_below="@+id/etPassword"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="56dp"
    android:layout_alignRight="@+id/etPassword"
    android:layout_alignEnd="@+id/etPassword"
    android:clickable="true"
    android:onClick="OnClickListener" />

RegisterRequest :注册请求:

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class RegisterRequest extends StringRequest {

 private static final String REGISTER_REQUEST_URL = "http://app.comxa.com/register.php";
private Map<String, String> params;

 public RegisterRequest(String username, String password, Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("username", username);
    params.put("password", password);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

in xml, attribute android:onClick must content the name of your method instead of listener name, for instance:在 xml 中,属性android:onClick必须包含您的方法名称而不是侦听器名称,例如:

android:onClick="mybutton"

and in class:在课堂上:

public void mybutton(View v){
    if(v.getId() == R.id.mybutton){
        //handle the click here and make whatever you want
  }
}

The other way to do that is using onClickListener as follows:另一种方法是使用 onClickListener ,如下所示:

in xml:在xml中:

android:id="@+id/mybutton"

and in class:在课堂上:

Button mybutton = (Button) findViewById(R.id.mybutton);
mybutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //your code here
    }
});

You have two choices to do this:您有两种选择来执行此操作:

  1. Register onClickListener for your button and without onClick attribute in xml.为您的按钮注册 onClickListener 并且在 xml 中没有 onClick 属性。
  2. create method with input(View view) in java class and call the method name in your xml onClick attribute.在 java 类中使用 input(View view) 创建方法,并在 xml onClick 属性中调用方法名称。 In this way you dont need to register any listener for buttons.通过这种方式,您不需要为按钮注册任何侦听器。

Your button looks like it should work, but for clarity, you can check this你的按钮看起来应该可以工作,但为了清楚起见,你可以检查这个

bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("HELLO", "Register Button Clicked");

Now, for reasons that "it doesn't work", you should check your Volley code.现在,由于“它不起作用”的原因,您应该检查您的 Volley 代码。 In particular, verify that the third parameter of RegisterRequest (the callback) is actually being used and executed.特别是,验证RegisterRequest的第三个参数(回调)实际上正在使用和执行。

You can verify that the callback is called with您可以验证回调是否被调用

@Override
public void onResponse(String response) {
    Log.d("HELLO", response);
    try {
        JSONObject jsonResponse = new JSONObject(response);

All you need to do is look at the logcat for those messages.您需要做的就是查看这些消息的 logcat。

You have two choices to do this:您有两种选择来执行此操作:

Register onClickListener for your button and without onClick attribute in xml.为您的按钮注册onClickListener并且在 xml 中没有onClick属性。 create method with input(View view) in Java class and call the method name in your xml onClick attribute.在 Java 类中使用input(View view)创建方法,并在您的 xml onClick属性中调用方法名称。 In this way you don't need to register any listener for buttons.通过这种方式,您无需为按钮注册任何侦听器。

I figured the solution out, the problem lies in your PHP script. 我想出了解决方案,问题出在您的PHP脚本中。 You probably want to check your domain name http://yourdomain.com/yourphpScript.php in JSONLint website and check whether it is valid JSON or not. 您可能想要在JSONLint网站中检查域名http://yourdomain.com/yourphpScript.php ,并检查其是否为有效的JSON。 Or post your PHP script. 或发布您的PHP脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM