简体   繁体   English

Android Studio-不显示提示文本和普通文本的EditText文本

[英]Android Studio - EditText text , both hint and normal text , not displaying

I know this question has been asked several times, and with different solutions, but I have tried all I could find here, and none worked. 我知道这个问题已经被问了好几次了,并且有不同的解决方案,但是我已经尽力在这里找到了,但是没有一个可行。 As the title suggest, in Android Studio, my EditText widget's field isn't showing any type of text, even what it has by default (like the hint and default text). 如标题所示,在Android Studio中,我的EditText小部件的字段未显示任何类型的文本,甚至没有默认显示的内容(例如提示和默认文本)。 The text is there if I print it out , but not displaying. 如果我将其打印出来,则文本在那里,但不显示。 It is for a simple login page. 这是一个简单的登录页面。

This is the xml file : 这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:text="Name : "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="40dp"
        android:id="@+id/atcoNameTag" />

    <TextView
        android:text="Password : "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/atcoNameTag"
        android:layout_marginTop="30dp"
        android:id="@+id/passTag"
        android:layout_alignStart="@+id/atcoNameTag"/>

    <TextView
        android:text="Role : "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/roleNameTag"
        android:layout_below="@+id/passTag"
        android:layout_alignStart="@+id/atcoNameTag"/>

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:ems="10"
        android:layout_above="@+id/passTag"
        android:layout_centerHorizontal="true"
        android:id="@+id/insertName"
        android:layout_alignTop="@+id/atcoNameTag"
        android:layout_toEndOf="@+id/atcoNameTag"
        android:layout_alignStart="@+id/insertPass"
        android:hint="username"
        android:cursorVisible="true"/>

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_alignBottom="@+id/passTag"
        android:layout_toEndOf="@+id/passTag"
        android:id="@+id/insertPass"
        android:layout_alignTop="@+id/passTag"
        android:hint="password"
        android:cursorVisible="true"/>

    <Spinner
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_alignTop="@+id/roleNameTag"
        android:layout_toEndOf="@+id/roleNameTag"
        android:layout_alignStart="@+id/insertPass"/>

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loginButton"
        android:onClick="onClick"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="35dp" />


</RelativeLayout>

And this is the java file : 这是java文件:

package mecals.mecalsapp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_page);
Button nameBtn = (Button) findViewById(R.id.loginButton);
nameBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        EditText nameEntry = (EditText) findViewById(R.id.insertName);
        nameEntry.setTextColor(Color.BLACK);
        String name = nameEntry.getText().toString();


        //TODO username checking, username not showing , need devC Team's work
        if (name.length() > 0) {
            Toast toastYes = Toast.makeText(getApplicationContext(), "Hi there, " + name + "!", Toast.LENGTH_SHORT);
            toastYes.show();

            Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
            startActivity(intent);

        } else {

            Toast toastNo = Toast.makeText(getApplicationContext(), "Please insert name !", Toast.LENGTH_SHORT);
            toastNo.show();
        }


        EditText passwordEntry = (EditText) findViewById(R.id.insertPass);
        passwordEntry.setTextColor(Color.BLACK);
        String pass = passwordEntry.getText().toString();
        //TODO password checking , need devC Team's work
    }
});
}
}

And these are the solutions I have tried , to no avail : 这些是我尝试过的解决方案,但无济于事:

Difference between content_main.xml and activity_main.xml? content_main.xml和activity_main.xml之间的区别?

Android edittext typed text not showing Android edittext输入的文字未显示

Android EditText typed value not showing Android EditText输入的值未显示

Text not displayed in Android editText when changing android:hint to android:text 将android:hint更改为android:text时,Android editText中未显示文本

EditText in Android doesn't show text when typing while using the on-screen keyboard 使用屏幕键盘输入时,Android中的EditText不显示文本

Edit Text hints not appearing on later version SDK 编辑文本提示未出现在更高版本的SDK上

edittext not showing the typed text in android edittext未在Android中显示键入的文本

In short, I have 简而言之,我有

-set the text colour to Black , with nameEntry.setTextColor(Color.BLACK); -将文本颜色设置为Black,名称为Entry.setTextColor(Color.BLACK);

-I have added android:windowSoftInputMode="adjustPan" in the manifest -我在清单中添加了android:windowSoftInputMode =“ adjustPan”

-I have NOT used something like android:gravity , just android:layout -我没有使用android:gravity之类的东西,只是android:layout

-the cursor is set to visible -光标设置为可见

-I even checked the height of the widget -我什至检查了小部件的高度

-I checked the size of the text with android:ems to be 10 -我用android:ems检查了文本的大小为10

-and the input type is set to text for the first field and textPassword for the second -并且将输入类型设置为第一个字段的text和第二个字段的textPassword

Help ! 救命 !

PS : before anyone asks , my Android Studio version is 2.2.3 , and the API I am using is min 21. PS:在有人问之前,我的Android Studio版本是2.2.3,而我正在使用的API至少是21。

The problem is that the height of the EditText field is much to small to display the text. 问题在于EditText字段的高度要小得多才能显示文本。 This is present because both the top and the bottom of the EditText field are set according to the TextView left of the EditText. 这是因为EditText字段的顶部和底部都是根据EditText左侧的TextView设置的。 But since the height of the TextView is smaller than the normal height of the EditText you force it to be very narrow (and so there is not enough space for the Text). 但是,由于TextView的高度小于EditText的正常高度,因此您将其强制非常窄(因此Text的空间不足)。 If you remove this line 如果您删除此行

  android:layout_alignTop="@+id/atcoNameTag" 

you can see that the text gets displayed normally. 您会看到文本正常显示。

There are actually several ways to fix this: 实际上,有几种方法可以解决此问题:

  • you could increase the text size of the TextViews 您可以增加TextViews的文本大小
  • you could add enough padding on top and on the bottom of the TextViews 您可以在TextView的顶部和底部添加足够的填充
  • you could decrease the text size of the EditText 您可以减小EditText的文本大小
  • you could decouple the height of the EditText from the TextView 您可以将EditText的高度与TextView分离

It is probably a good idea to go with the last solution and put the Textview and EditText fields that belong together into a separate LinearLayout and remove the align calls like this: 最好采用最后一个解决方案,将属于在一起的Textview和EditText字段放入单独的LinearLayout中,然后删除align调用,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginStart="30dp"
        android:layout_marginTop="40dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:weightSum="100"
        >

        <TextView
            android:layout_weight="30"
            android:text="Name : "
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/atcoNameTag" />

        <EditText
            android:layout_weight="60"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:id="@+id/insertName"
            android:hint="username"
            android:cursorVisible="true"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/second"
        android:layout_below="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginStart="30dp"
        android:weightSum="100"
        >

        <TextView
            android:layout_weight="30"
            android:text="Password : "
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/passTag"/>

        <EditText
            android:layout_weight="60"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:id="@+id/insertPass"
            android:hint="password"
            android:cursorVisible="true"/>

        </LinearLayout>


    <LinearLayout
        android:layout_below="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginStart="30dp"
        android:weightSum="100"
        android:layout_marginTop="10dp"
        >

        <TextView
            android:layout_weight="30"
            android:text="Role : "
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/roleNameTag"/>

        <Spinner
            android:layout_weight="60"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/spinner"/>

        </LinearLayout>

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loginButton"
        android:onClick="onClick"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="35dp" />


</RelativeLayout>

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

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