单击Action not not on Button - Java和Android Studio

时间:2016-03-18 17:25:03

标签: java android mobile

我觉得这是一个不得不问这个问题的白痴,但我正在研究一个需要登录的简单应用程序。如果输入了正确的凭据,则会重定向到设置/首选项活动。如果不是,则显示错误。简单吧?但是,每当我点击按钮时,都没有任何反应。

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

   <?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>

我真的非常感谢任何帮助!

4 个答案:

答案 0 :(得分:0)

尝试这样的事情。将代码包装在方法中并在按钮单击事件中调用它。

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);
            }
        });
}

将此包装在方法

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();
                }
            });

        }
}

答案 1 :(得分:0)

onCreate方法是初始化活动的地方。您验证的变量将始终为false。在onCreate完成后执行按钮单击的回调。将您的逻辑移动到其他方法或放入按钮回调。

答案 2 :(得分:0)

使用View.OnClickListener而不是Button.onClickListener

答案 3 :(得分:0)

您必须编写导航到设置屏幕的代码或在OnClickListener内显示错误对话框。目前它在它之外。试试这个 -

 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();
                }
            });

        }
            }
        });