Ввод символов в EditText - PullRequest
0 голосов
/ 14 октября 2018

Существует фрагмент 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>

Код фрагмента:

package asus.example.com.fitnessapp;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class CalculatorFragment extends Fragment implements View.OnClickListener {

    TextView textView;
    EditText eWeight, eHeight;
    int nWeight, nHeight;

    public CalculatorFragment() {
        // Required empty public constructor
    }

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_calculator, container, false);
        Button button = v.findViewById(R.id.count);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eWeight = v.findViewById(R.id.weight);
                eHeight = v.findViewById(R.id.height);
                nWeight = Integer.parseInt(eWeight.getText().toString());
                nHeight = Integer.parseInt(eHeight.getText().toString());
                if (nWeight-nHeight<100) {
                    textView.setText("Normal weight");
                }
            }
        });

        return v;
    }

}

ОБНОВЛЕНИЕ

стили.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

На самом деле я думаю, что эмулятор может вызвать эту проблему, потому что, когда я ввожу цифры, я могу изменить положение курсора, поэтому здесь что-то есть.Но это не показано.Но в то же время, когда я пытаюсь проанализировать int информацию из этого editTexts, это вызывает ошибку, поскольку здесь ничего нет.

1 Ответ

0 голосов
/ 14 октября 2018

Я проверил предоставленные вами коды, возникла проблема с макетом.Просто добавьте эту строку в ваш XML-блок EditText: android:layout_height="wrap_content" вместо того, чтобы жестко зафиксировать его в 20dp, что не считается хорошим измерением высоты для поля EditText.

Согласно рекомендациям разработчика Google Android,минимальная высота для EditText должна быть 50dp.Таким образом, вы можете также рассмотреть возможность изменения высоты EditText на 50dp также вместо "wrap_content"


Таким образом, модифицированный 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="wrap_content"
        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="wrap_content"
        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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...