简体   繁体   English

Android Studio 无法识别输入字段“从未使用过字段”

[英]Android Studio not recognizing input fields "Field is never used"

so I'm trying to learn Android Studio and this is the first time I've done anything.所以我正在尝试学习 Android Studio,这是我第一次做任何事情。 I'm trying to make a simple login app and I just added two EditText fields but they are not being recognized.我正在尝试制作一个简单的登录应用程序,我刚刚添加了两个EditText字段,但它们未被识别。

I get this error;我收到这个错误;

Cannot resolve symbol EditText

Although I made sure I have an EditText field in my application.虽然我确保我的应用程序中有一个EditText字段。

This is the Java code:这是Java代码:

package localhost.testloginphpapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

EditText usernameField, passwordField;

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

And the XML code:和 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="localhost.testloginphpapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<EditText
    android:id="@+id/usernameField"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Username"
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="133dp" />

<EditText
    android:id="@+id/passwordField"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword"
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="190dp" />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    tools:layout_editor_absoluteX="148dp"
    tools:layout_editor_absoluteY="245dp" />

</android.support.constraint.ConstraintLayout>

Thanks a lot.非常感谢。

您必须在包下但在类之前编写以下代码:

import android.widget.EditText;

EditText is a widget. EditText 是一个小部件。 You can use it in XML.您可以在 XML 中使用它。 But in Java, You have to import its class by adding但是在 Java 中,您必须通过添加来导入它的类

android.widget.EditText

on top of the file before class declaration.在类声明之前的文件顶部。

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

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