在 EditText 中输入符号

片段中有一个 EditText。用户应在此处输入一个数字(输入格式为数字)。但是有一个问题:editText 中没有显示任何内容。用户可以输入,但看不到数字。怎么了?


片段的 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"

    tools:context=".CalculatorFragment">


    <!-- TODO: Update blank fragment layout -->


    <TextView

        android:id="@+id/text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/calculator_text"

        android:layout_marginLeft="16dp"

        android:layout_marginRight="16dp"

        android:layout_marginTop="16dp" />


    <EditText

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:id="@+id/weight"

        android:layout_below="@+id/text"

        android:layout_marginTop="20dp"

        android:layout_marginLeft="16dp"

        android:layout_marginRight="16dp"

        android:hint="@string/weight"

        android:inputType="number"/>


    <EditText

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:id="@+id/height"

        android:layout_below="@+id/weight"

        android:layout_marginTop="20dp"

        android:layout_marginLeft="16dp"

        android:layout_marginRight="16dp"

        android:hint="@string/height"

        android:inputType="number"/>


    <Button

        android:id="@+id/count"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_below="@id/height"

        android:layout_marginTop="30dp"

        android:text="@string/count"

        android:onClick="onClick"/>


    <TextView

        android:id="@+id/textview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/count"

        android:layout_marginTop="20dp"/>


</RelativeLayout>


慕少森
浏览 144回答 1
1回答

qq_遁去的一_1

我已经测试了您提供的代码,布局有问题。只需将此行添加到您的 EditText xml 块:android:layout_height="wrap_content"而不是将其硬固定到20dp,这不被认为是 EditText 字段的良好高度尺寸。根据 Google Android 开发人员的建议,EditText 的最小高度应为50dp. 所以你也可以考虑将 EditText 的高度改为50dpalso 而不是"wrap_content"因此修改后的 xml 如下所示:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".CalculatorFragment">&nbsp; &nbsp; <!-- TODO: Update blank fragment layout -->&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/text"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/calculator_text"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="16dp" />&nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/weight"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_below="@+id/text"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="20dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:hint="@string/weight"&nbsp; &nbsp; &nbsp; &nbsp; android:inputType="number"/>&nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/height"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_below="@+id/weight"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="20dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:hint="@string/height"&nbsp; &nbsp; &nbsp; &nbsp; android:inputType="number"/>&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/count"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerHorizontal="true"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_below="@id/height"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="30dp"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/count"&nbsp; &nbsp; &nbsp; &nbsp; android:onClick="onClick"/>&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textview"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_below="@+id/count"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="20dp"/></RelativeLayout>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java