简体   繁体   English

即使输入了文本,为什么Android EditText也显示为空?

[英]Why does Android EditText shows empty even though there is text entered?

I'm trying to test two EditText views to see if they contain the correct username and password. 我正在尝试测试两个EditText视图,以查看它们是否包含正确的用户名和密码。 The problem I'm having is the EditText inputs show "" even though I've entered the text into the views. 我遇到的问题是即使我已将文本输入视图,EditText输入仍显示为“”。 What am I doing wrong here? 我在这里做错了什么?

package com.example.cs984x.iteration_one;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    Button btn1 = (Button) findViewById((R.id.button));
    final EditText userName = (EditText) findViewById((R.id.editText));
    final EditText pw = (EditText) findViewById((R.id.editText1));
    final TextView result = (TextView) findViewById(R.id.textView3);

    final String u = userName.getText().toString();

    final String p = pw.getText().toString();


    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if ((u.equals("Android")) && (p.equals("123123"))) {

                result.setText("Success!");
            }
            else {
                result.setText("Failed!");
            }
        }
    });

You are initializing u and p during creation of your activity (at which point, the editText is empty). 您正在创建活动时初始化u和p(此时,editText为空)。 You need to fetch the values inside your event handler (inside onClick): 您需要获取事件处理程序内部的值(在onClick内部):

btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if ((userName.getText().toString().equals("Android")) && (pw.getText().toString().equals("123123"))) {

                result.setText("Success!");
            }
            else {
                result.setText("Failed!");
            }
        }
    });

The Issue: 问题:

  1. You are checking against u , which was set at the initialization of the activity when the text was blank. 您正在检查u ,它是在文本空白时在活动初始​​化时设置的。

EditText userName = (EditText) findViewById((R.id.editText));
EditText pw = (EditText) findViewById((R.id.editText1));
TextView result = (TextView) findViewById(R.id.textView3);


btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

     String u = userName.getText().toString();
     String p = pw.getText().toString();

        if ((u.equals("Android")) && (p.equals("123123"))) {

            result.setText("Success!");
        }
        else {
            result.setText("Failed!");
        }
    }
});

暂无
暂无

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

相关问题 当 EditText 输入为空时,Android 应用程序崩溃 - Android Application Crashes When EditText Is Entered Empty 为什么即使我在请求期间向其中添加了元素,此代码也会显示一个空数组列表? - why this code shows an empty array list even though i added elements to it during request? 为什么 Java.exe 突然无法找到文件/模块,即使 Windows Explorer 显示它们存在 - Why does Java.exe suddenly fail to find files/modules even though Windows Explorer shows they exist EditText随着文本的输入而增长 - EditText is growing as text entered 即使内容是XML,POST contentType也始终显示text / plain - POST contentType always shows text/plain even though content is XML 即使调试器显示测试为真,最终循环也不会迭代 - Final loop does not iterate, even though debugger shows test is true 空的 editText 字段在所有字段上显示错误 [Android] - Empty editText field shows error on all fields [Android] Android 通知显示即使时间已经过去 - Android Notification Shows Even Though It's Time Has Already Passed 为什么 getNdefMessage() 会返回 null 即使调试器显示 ndef object 的消息有效负载应该是正确格式的? - Why does getNdefMessage() returns null even though debugger shows ndef object has a message payload that is supposedly correctly formatted? android测试-EditText即使有字符也返回空 - android testing - EditText returning empty even when it has characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM