简体   繁体   English

吐司不显示

[英]Toast is not showing

I want to make a small login and it should give a popup (Toast) if you have the password correct. 我想进行一个小的登录,如果密码正确,它应该会弹出一个窗口(Toast)。 The login design and activity_main.xml are ok, but something goes wrong in my MainActivity.java. 登录设计和activity_main.xml都可以,但是MainActivity.java中出了点问题。 It doesn't show any errors and the app is not crashing but it just won't give me the Toast action. 它没有显示任何错误,该应用程序也没有崩溃,但它没有给我Toast操作。

I am following this tutorial: https://examples.javacodegeeks.com/android/android-login-example/ 我正在关注本教程: https : //examples.javacodegeeks.com/android/android-login-example/

activity_main.xml activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 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"
 tools:context="com.localhost.login.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Please fill in your password."
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:gravity="center"
    android:id="@+id/digitcode"
    android:layout_marginTop="47dp"
    android:textSize="15dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_login_submit"
    android:id="@+id/loginButton"
    android:layout_below="@+id/digitcode"
    android:layout_marginTop="47dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

MainActivity.java MainActivity.java

package com.localhost.login;

import android.support.v7.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText passcode;
private Button login;

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

// Check login
public void checkLogin(View view) {
    if (passcode.getText().toString().equals("aa")) {
        Toast.makeText(getApplicationContext(), "Hi Man!!", Toast.LENGTH_SHORT).show();
    }
}

// Declare variables
private void setupVariables() {
    passcode = (EditText) findViewById(R.id.digitcode);
    login = (Button) findViewById(R.id.loginButton);
}


}

Can someone give me a push in the right direction? 有人可以向我推正确的方向吗?

尝试在xml文件的<Button>中添加此行:

<android:onClick="checkLogin"

我查看了您的代码,一切都很完美,我观察到的一件事是,checkLogin方法没有在任何地方调用,因此您需要在setupVariables方法内部调用checkLogin方法。

I tend to find the XML onClick settings hidden and hard to track-down when you start making a lot of them. 当您开始进行很多设置时,我倾向于发现XML onClick设置是隐藏的并且很难追踪。

Alternatively, you can just set the button click in the Java code. 或者,您可以仅在Java代码中设置按钮单击。

private void setupVariables() {
    passcode = (EditText) findViewById(R.id.digitcode);
    login = (Button) findViewById(R.id.loginButton);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TextUtils.equals(passcode.getText(), "aa")) {
                Toast.makeText(getApplicationContext(), "Hi Man!!", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

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

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