In the fragment there is an EditText where the user must enter any number (the input format is numeric). However, for some reason, the input does not occur. The numbers on the keyboard are pressed, and the characters in EditText are not displayed. What is the problem?

Here is the xml fragment:

<?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> 

And here is the fragment code:

UPDATE2:

 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 { 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; } } 

    1 answer 1

    Judging by your markup, you have two text fields in which only numbers should be entered. But the problem is that they are not initialized in your fragment. That is, what you have at the moment - you enter your numbers in the input fields, but the program does not see them, they are only on the markup. In order for everything to work you need to do this:

     EditText eTxt1, eTxt2; 

    then you need to find them by id:

     eTxt1 = v.findViewById(R.id.weight); eTxt2= v.findViewById(R.id.height); 

    and further it is necessary to pull out data from them. To do this, enter two string variables that will be equal to what you entered in the fields:

     String a = eTxt1.getText().toString(); String b = eTxt2.getText().toString(); 

    and then you already convert the entered text into a number and do with it what you need. Maybe there is a more elegant way to immediately pull the numbers from the fields, but at the moment I use this) I hope I understand the meaning. Logically it should work, but at the moment there is nothing to check. If someone has a better solution than mine, I will be happy to get acquainted with it. I hope I helped you, if you have questions - do not hesitate and ask, I will help as much as I can. Good luck :)

    update

    This is what you should end up with:

     public class CalculatorFragment extends Fragment { TextView textView; Button button; 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 = v.findViewById(R.id.count); eWeight = v.findViewById(R.id.weight); eHeight = v.findViewById(R.id.height); textView = v.findViewById(R.id.textview); // здесь я так и не понял какой именно вам нужен текствью button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nWeight = Integer.parseInt(eWeight.getText().toString()); nHeight = Integer.parseInt(eHeight.getText().toString()); if (nWeight-nHeight<100) { textView.setText("Normal weight"); } } }); return v; } } 
    • I agree with your logic, but for some reason it still does not work. I added a modified version of the fragment code to the question - Sergei Mikhailovskii
    • one
      I suggest you do everything in the onCreate () function can help, put a button click listener there. Because it seems to me that it is not necessary to track button presses by id, while you have only one button - Andrew Goroshko
    • Well, I did this in case I’ll add more buttons. But in general, yes, it is reasonable - Sergei Mikhailovskii
    • one
      where did you declare you a textView? - Andrew Goroshko
    • one
      @SergeiMikhailovskii, updated the answer - Andrew Goroshko