首页 > 解决方案 > getText 方法的输出不能存储在 String 变量中,怎么办?

问题描述

我无法从 getText 方法将数据存储在变量中,它正在返回数据,但如果我将 et1.getText.toString 放入 toast 而不是变量它的工作,则变量不存储数据我不知道为什么会发生这种情况请帮忙出去

    package com.example.mad_lab_one_addition_app;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    //import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText et1,et2;
        Button btn;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            et1 = findViewById(R.id.edittextone);
            et2 = findViewById(R.id.edittexttwo);
            btn = findViewById(R.id.button1);
    
            add();
        }
    
        public void add(){
    
    
            String string = et1.getText().toString();
            // String str2 = et2.getText().toString();
    
    //        int a = Integer.parseInt(str);
    //        int b = Integer.parseInt(str2);
    //        int c = a+b;
    
    
    
            btn.setOnClickListener(v -> Toast.makeText(MainActivity.this,string,Toast.LENGTH_LONG).show());
    
        }
    }
    </code>
    </pre>


it is java file !
<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=".MainActivity">

    <EditText
        android:id="@+id/edittextone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="44dp"
        android:ems="10"
        android:hint="@string/enter_first_number"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/edittexttwo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:autofillHints=""
        android:importantForAutofill="no" />

    <EditText
        android:id="@+id/edittexttwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="218dp"
        android:ems="10"
        android:hint="@string/enter_second_number"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="61dp"
        android:text="@string/add"
        app:backgroundTint="@null"
        android:background="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edittexttwo" />
</androidx.constraintlayout.widget.ConstraintLayout>

它的 XML !我正在尝试打印吐司,但打印的是空吐司。谁能帮忙?

标签: javaandroidgettext

解决方案


你的EditText一开始是空的。您应该输入文本EditText然后单击按钮并将数据存储到变量中。请遵循以下方法:

public class MainActivity extends AppCompatActivity {

    EditText et1,et2;
    Button btn;


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

        et1 = findViewById(R.id.edittextone);
        et2 = findViewById(R.id.edittexttwo);
        btn = findViewById(R.id.button1);

        btn.setOnClickListener(v ->
                String temp = et1.getText().toString();
                Toast.makeText(MainActivity.this,temp,Toast.LENGTH_LONG).show());

                // Do whatever you want with the temp.
    }

}

推荐阅读