无法从 EditText 中获取文本

Unable to get Text out of EditText

所以我想读取 EditText 字段的输入。它不工作。我调试了它,我看到那里列出了 EditText 视图。但是当我把它打印到控制台时,它是空的。我不知道为什么,因为我确实在里面放了一段文字。还有,ID是对的,真不知道哪里不对

这是 EditText 所在的视图:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".counters.AddCounter">

    <TextView
        android:id="@+id/txtNewCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/allerta"
        android:text="@string/addNewCounter"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnSaveCounter"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/allerta"
        android:text="@string/save"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtCounterName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:fontFamily="@font/allerta"
        android:text='@string/newCounterName'
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtNewCounter" />

    <EditText
        android:id="@+id/inpuTextNewCounterName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:contentDescription="@string/name"
        android:ems="10"
        android:hint="@string/name"
        android:inputType="textPersonName"
        android:singleLine="true"
        android:text="Cookies don't"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtCounterName" />

    <TextView
        android:id="@+id/txtCounterEntryValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:fontFamily="@font/allerta"
        android:text="@string/entryNum"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inpuTextNewCounterName" />

    <EditText
        android:id="@+id/numEntryNum"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:hint="@string/number"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtCounterEntryValue" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我用来评估表单的代码:


public class AddCounter extends AppCompatActivity {

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

        //Setting save button action listener
        findViewById(R.id.btnSaveCounter).setOnClickListener(new ActionListeners().getSaveCounter());
    }

    public static void evaluateCounter(View view) throws NoNameException, NumberOutOfBoundException {
        //Getting context
        Context context = view.getContext();

        //Counter Name
        View viewEdit = View.inflate(context, R.layout.activity_add_counter, null);
        EditText counterName = viewEdit.findViewById(R.id.inpuTextNewCounterName);

        System.out.println("Cookies fly like:" + counterName.getText().toString() + "!");

        //Input to string
        String name = counterName.getText().toString();

        //If no name was specified exception thrown
        if (name.equals("")) {
            throw new NoNameException(context.getString(R.string.noNameSpecified));
        }

        //get entry number
        EditText numberView = View.inflate(context, R.layout.activity_add_counter, null).findViewById(R.id.numEntryNum);

        //Number
        int number = 0;
        //If nothing specified
        if (!(numberView.getText().toString().equals(""))) {
            if (Integer.parseInt(numberView.getText().toString()) > 1000 || Integer.parseInt(numberView.getText().toString()) < -1000) {
                throw new NumberOutOfBoundException(context.getString(R.string.numOutOfBound));
            }

        }
        //New Counter Object
        Counter counter = new Counter(number, name);

        //storing counter
        ToJson toJson = new ToJson();
        toJson.storeCounter(counter, context);
    }
}

正如您在此处看到的,此 if 陈述始终为真。 看看,文字长什么样,我打印了一段文字:

System.out.println("Cookies fly like:" + counterName.getText().toString() + "!");

我确实在输入中输入了一个词,但输出为空,如您所见:

I/System.out: Cookies fly like:!

这是我的 ActionListener:¨

public class ActionListeners {
    private OnClickListener saveCounter = v -> {
        try {
            //Evaluate Counter
            AddCounter.evaluateCounter(v);
        } catch (NoNameException e) {
            //If no name was specified
            CounterMethods.makeSnackbar(v, Snackbar.LENGTH_SHORT, HapticFeedbackConstants.REJECT, v.getContext().getString(R.string.noNameSpecified));
            e.printStackTrace();
        } catch (NumberOutOfBoundException e) {
            //If entry is higher or lower than available
            CounterMethods.makeSnackbar(v, Snackbar.LENGTH_SHORT, HapticFeedbackConstants.REJECT, v.getContext().getString(R.string.numOutOfBound));
        }
    };

    public OnClickListener getSaveCounter() {
        return this.saveCounter;
    }

}

如您所见,我确实在 xml 文件中将文本设置为“Cookies can't fly”,但是,如果我在应用程序中将文本更改为不同的内容,输出是我在 XML.

中设置的文本
I/System.out: Cookies fly like:Cookies can't fly!

有什么问题?

试试这个

public class AddCounter extends AppCompatActivity{


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

            //declare Counter Name EditText
            EditText counterName = findViewById(R.id.inpuTextNewCounterName);

            //declare btnSaveCounter Button
            Button btnSaveCounter = findViewById(R.id.btnSaveCounter);
            //declare  get entry number EditText
            EditText numberView = findViewById(R.id.numEntryNum);


            //btnSaveCounter setOnClickListener
            btnSaveCounter.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {

                    //Input to string
                    String name = counterName.getText().toString();
                    System.out.println("Cookies fly like:" + name + "!");

                    //If no name was specified
                    if (name.equals("")){
                        System.out.println( "getString(R.string.noNameSpecified)" );//todo
                    }
                    
                    //If nothing specified
                    String numbrv = numberView.getText().toString();
                    if (!(numbrv.equals(""))){
                        if (Integer.parseInt(numbrv) > 1000 || Integer.parseInt(numbrv) < -1000) {
                            System.out.println( "getString(R.string.numOutOfBound)" );//todo
                        }
                    }

                    //Number
                    int number = 0;
                    
                    //New Counter Object
                    Counter counter = new Counter(number, name);

                    //storing counter
                    ToJson toJson = new ToJson();
                    toJson.storeCounter(counter, context);
                    


                }  });

            




        }

        

    }