简体   繁体   English

单击“无法在按钮上使用操作”-Java和Android Studio

[英]Click Action not Working on Button - Java and Android Studio

I feel like an idiot for having to ask this, but I am working on a simple application that requires login. 我不得不问这个问题,觉得自己很蠢,但是我正在开发一个需要登录的简单应用程序。 If the correct credentials are entered, it redirects to the settings/preferences activity. 如果输入了正确的凭据,它将重定向到设置/首选项活动。 If not, it displays an error. 如果不是,则显示错误。 Simple, right? 简单吧? However, I whenever I click the button, nothing happens. 但是,无论何时单击按钮,都不会发生任何事情。

login.java login.java

 import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class login extends AppCompatActivity {
    EditText userName;
    EditText password;
    Boolean verified = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Button loginButton = (Button) findViewById(R.id.loginButton);
        userName = (EditText) findViewById(R.id.userNameField);
        password = (EditText) findViewById(R.id.passwordField);

        loginButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((userName.getText().toString() != "bkAdmin") || (password.getText().toString() != "midAmBK")) {
                    verified = false;
                } else {
                    verified = true;
                }
            }
        });
        SharedPreferences preferences = getSharedPreferences("config.xml", MODE_PRIVATE);
        if (verified) {
            Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("loggedIn", true);
            editor.commit();
            if (preferences.getBoolean("config", false)) {
                editor.putBoolean("config", true);
                editor.commit();
            }
            startActivity(intent);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Invalid Username or Password");

            builder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

        }

    }
}

content_login.xml content_login.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="midamcorp.com.burgerkingapp.login"
    tools:showIn="@layout/activity_login">
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow android:layout_marginBottom="50dp">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:minWidth="200dp"
            android:text=" Username:" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userNameField"
        android:minWidth="200dp"
       />

    </TableRow>
    <TableRow android:layout_marginBottom="50dp">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:minWidth="200dp"
            android:text=" Password:" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"

        android:minWidth="200dp"
        android:id="@+id/passwordField"
/>
</TableRow>
<TableRow>
    <Button
        android:id="@+id/loginButton"
        android:text = "Login!" />

</TableRow>
</TableLayout>
</RelativeLayout>

I truly, truly appreciate any help! 我真的非常感谢您的帮助!

Try something like this. 尝试这样的事情。 wrap you code in a method and call it inside your button click event. 将您的代码包装在一个方法中,并在您的按钮click事件中调用它。

loginButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((userName.getText().toString() != "bkAdmin") || (password.getText().toString() != "midAmBK")) {
                    verified = false;
                } else {
                    verified = true;
                }
                completeRegistration(verified);
            }
        });
}

Wrap this in a method 用一种方法包装

private void completeRegistration(boolean verified){
        if (verified) {
            Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("loggedIn", true);
            editor.commit();
            if (preferences.getBoolean("config", false)) {
                editor.putBoolean("config", true);
                editor.commit();
            }
            startActivity(intent);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Invalid Username or Password");

            builder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

        }
}

The onCreate method is where you initialize the activity. 您可以在onCreate方法中初始化活动。 Your verified variable will always be false. 您已验证的变量将始终为false。 The callback of button click is executed after the onCreate was finished. onCreate完成后,将执行按钮单击的回调。 Move your logic to other method or put inside the button callback. 将逻辑移至其他方法或放入按钮回调中。

使用View.OnClickListener而不是Button.onClickListener

You have to write the code for navigating to the settings screen or showing the error dialog inside OnClickListener . 您必须编写代码以导航到设置屏幕或在OnClickListener显示错误对话框。 At present it is outside it. 目前它不在外面。 Try this - 尝试这个 -

 import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class login extends AppCompatActivity {
    EditText userName;
    EditText password;
    Boolean verified = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Button loginButton = (Button) findViewById(R.id.loginButton);
        userName = (EditText) findViewById(R.id.userNameField);
        password = (EditText) findViewById(R.id.passwordField);
loginButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((userName.getText().toString() != "bkAdmin") || (password.getText().toString() != "midAmBK")) {
                    verified = false;
                } else {
                    verified = true;
                }

 SharedPreferences preferences = getSharedPreferences("config.xml", MODE_PRIVATE);
        if (verified) {
            Intent intent = new Intent(login.this, midamcorp.com.burgerkingapp.preferences.class);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("loggedIn", true);
            editor.commit();
            if (preferences.getBoolean("config", false)) {
                editor.putBoolean("config", true);
                editor.commit();
            }
            startActivity(intent);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
            builder.setMessage("Invalid Username or Password");

            builder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

        }
            }
        });

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

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