getText 方法的输出不能存储在 String 变量中,怎么办?

the output of getText method cant be stored in the String variable, what to do?

我无法通过 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 ! 我正在尝试打印吐司,但它打印的是空吐司。 有人可以帮忙吗?

你的 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.
    }

}

在从中获取值之前,您应该始终检查 EditText 是否为空 如果你想实现一些简单的加法,比如c=a+b,你可以查看这段代码

public class MainActivity extends AppCompatActivity {

    EditText et1,et2;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

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


        btn.setOnClickListener(v ->{
            String num1 = et1.getText().toString();
            String num2 = et1.getText().toString();

            if(num1.isEmpty() || num2.isEmpty()){
                Toast.makeText(getApplicationContext(),"Enter a and b", Toast.LENGTH_SHORT).show();
            }else {
             add(Integer.parseInt(num1),Integer.parseInt(num2));
            }
        });


    }
    public void add(int a,int b){
        int c = a+b;
        Toast.makeText(getApplicationContext(),"Result "+c, Toast.LENGTH_LONG).show();
    }

}

此外,如果您使用数字进行加法,请将您的 EditText 输入类型更改为 xml

中的数字
android:inputType="number"

您只需初始化您的变量,这里我们将设置它的初始值null。那么一切都会好起来的。没有什么可做的了。这将解决您的问题。

EditText et1 = null;
EditText et2 = null;

如果您想了解更多,请按照步骤操作,否则不要。 如果你想在 Activity Create 上显示 Toast,只需制作 add 方法 runnable.

 public void add(final String string){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
        }
    });

    }

如果你想通过点击按钮显示-

btn.setOnClickListener(v -> Toast.makeText(MainActivity.this,string,Toast.LENGTH_LONG).show());

你的主要Activity-

public class MainActivity extends AppCompatActivity {

    EditText et1 = null;
    EditText et2 = null;

    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();
        String string = et1.getText().toString();
        //without user action
        add(string);
     //with user action
        btn.setOnClickListener(v -> Toast.makeText(MainActivity.this,string,Toast.LENGTH_LONG).show());

    }

    public void add(final String string){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
        }
    });

    }

}