Java 计算两个字符串时,它在TextView中返回为nullnull
对不起,这是我第一次在安卓系统开发方面做新手。我在做一个计算器,比如,用任何一个操作符加上这个数字 数字的范围是0-1,我选择1和2,因为按钮具有相同的java代码 代码 My.xmlJava 计算两个字符串时,它在TextView中返回为nullnull,java,android,Java,Android,对不起,这是我第一次在安卓系统开发方面做新手。我在做一个计算器,比如,用任何一个操作符加上这个数字 数字的范围是0-1,我选择1和2,因为按钮具有相同的java代码 代码 My.xml <TextView android:id="@+id/tvresult" android:layout_width="200dp" android:layout_height="155dp" android:layout_alignParentRight="true
<TextView
android:id="@+id/tvresult"
android:layout_width="200dp"
android:layout_height="155dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:maxLength="18"
android:maxLines="1"
android:textAlignment="center"
android:gravity="bottom"
android:textColor="@color/colorPrimary"
android:textSize="40sp"
android:visibility="visible"/>
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="80dp"
android:onClick="btn1_click"
android:text="1"
android:textSize="20sp"
android:backgroundTint="@color/colorPrimaryDark"
android:layout_above="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="80dp"
android:onClick="btn2_click"
android:text="2"
android:textSize="20sp"
android:backgroundTint="@color/colorPrimaryDark"
android:layout_above="@+id/button5"
android:layout_toRightOf="@+id/button1"
android:layout_toEndOf="@+id/button1" />
<Button
android:id="@+id/plus1"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:onClick="btnAdd_click"
android:text="+"
android:textSize="20sp"
android:backgroundTint="@color/colorPrimary"
android:textColor="@android:color/white"
android:layout_alignTop="@id/button3"
android:layout_toRightOf="@+id/button3"
android:layout_toEndOf="@+id/button3"/>
<Button
android:id="@+id/equals1"
android:layout_width="100dp"
android:layout_height="115dp"
android:onClick="btnEqual_click"
android:text="="
android:textSize="20sp"
android:backgroundTint="@color/colorPrimary"
android:layout_alignTop="@+id/clear1"
android:layout_toRightOf="@+id/clear1"
android:layout_toEndOf="@+id/clear1" />
只有整数值才能执行计算操作。 因此,首先必须将字符串值更改为整数,然后需要在其中添加一些值
String result = String.valueOf(Integer.valueOf(tvresult.getText().toString()) + 1);
您需要将字符串类型转换为int,如 Integer.parseINtnumber_字符串 在文本视图中设置为文本之前,必须键入int值,如
Stirng.valueofit_value编辑文本为空。使用android.text.TextUtils.isEmpty检查值是否为null或空 请在下面查找更新的代码:
如果要递增字符串,请不要向其添加1。您的期望值是多少?如果使用+运算符添加2个字符串,会发生什么情况?我需要将字符串转换为整数吗?“String.valuevalue”在括号值之前,它无法解析更新的答案。使用String.valueOfB,但它返回为nullnullnull我正在做一个计算器,我不需要这样做,然后您将从其他编辑文本或文本视图中获取值。请张贴完整的代码,包括布局和活动
String result = String.valueOf(Integer.valueOf(tvresult.getText().toString()) + 1);
public void btn1_click (View v) {
String currentTextData = tvresult.getText().toString();
if(android.text.TextUtils.isEmpty(currentTextData)) {
Toast.makeText(getApplicationContext(), "Enter value 1", Toast.LENGTH_SHORT).show();
} else {
int value = 0;
try {
value = Integer.parseInt(currentTextData);
} catch(Exception e) {}
value = value + 1;
String updatedValue = String.valueOf(value);
tvresult.setText(updatedValue);
}
}
public void btn2_click (View v){
String currentTextData = tvresult.getText().toString();
if(android.text.TextUtils.isEmpty(currentTextData)) {
Toast.makeText(getApplicationContext(), "Enter value 2", Toast.LENGTH_SHORT).show();
} else {
int value = 0;
try {
value = Integer.parseInt(currentTextData);
} catch(Exception e) {}
value = value + 2;
String updatedValue = String.valueOf(value);
tvresult.setText(updatedValue);
}
}
public void btnAdd_click (View v){
if(SystemClock.elapsedRealtime() - mlastClickTime < 1000){
return;
}
mlastClickTime = SystemClock.elapsedRealtime();
String val1 = tvresult.getText().toString();
val1 = val1 + "+";
add = true;
tvresult.setText(val1);
}
public void btnEqual_click(View v) {
if (add) {
val1 += val2;
tvresult.setText(val1);
}
}