java - 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 !我正在尝试打印吐司,但打印的是空吐司。谁能帮忙?
解决方案
你的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.
}
}
推荐阅读
- python - 没有名为“camelot.ext”的模块
- reactjs - 无法使用 @emotion/styled 访问样式化组件中的主题
- sql - 为什么选择大于某个日期的值也会返回匹配的日期?
- amazon-web-services - 通过堡垒限制对 AWS EC2 实例的访问
- php - html页面不断刷新
- iphone - Webpack DevServer iPhone 开发 - Set-Cookie 失败
- nestjs - 如何从 Nestjs 中的 aws s3 存储桶下载文件
- java - Stripe 与 Android 中的 IDEAL 特定集成
- apache-spark-sql - spark sql“插入覆盖”操作可以维护数据顺序吗?
- typescript - 打字稿:尝试用功能实现替换三元运算符