gpt4 book ai didi

java - 尝试使用多个 EditText 进行 Catch,并与 OnClick 进行交互

转载 作者:行者123 更新时间:2023-12-01 21:47:58 25 4
gpt4 key购买 nike

这是我的第一个问题,我对这个个人项目的经验很少,但我会尽力学习,所以这就是我的问题开始的地方。

我试图让我的应用程序在 EditText 没有用户输入时停止崩溃,因此我实现了 trycatch 方法。但是,我相信我可能对 Activity 的 4 个 editText 实现得很糟糕,并且破坏了 onClick 监听器访问变量和计算的能力。此次重组后,onClick 似乎无法访问 public void caudalinput。

package com.example.aguavida;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.InvocationTargetException;

public class G_Inflow_Rate extends AppCompatActivity {

EditText medidoreditText;
EditText T1editText;
EditText T2editText;
EditText T3editText;

int m;
int t1;
int t2;
int t3;

public double a;
public double result;

public void caudalinput(View view) {

Context context_caudal = getApplicationContext();
String exceptionMessage = "";
String exceptionMessage1 = "";
String exceptionMessage2 = "";
String exceptionMessage3 = "";

try{
medidoreditText = (EditText) findViewById(R.id.medidoreditText);
m = Integer.parseInt(medidoreditText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage = "Balde";
}

try{
T1editText = (EditText) findViewById(R.id.t1editText);
t1 = Integer.parseInt(T1editText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage1 = "time 1";
}

try{
T2editText = (EditText) findViewById(R.id.t2editText);
t2 = Integer.parseInt(T2editText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage2 = "time 2";
}

try{
T3editText = (EditText) findViewById(R.id.t3editText);
t3 = Integer.parseInt(T3editText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage3 = "time 3";
}

Toast.makeText(this, exceptionMessage + " " + exceptionMessage1 + " " + exceptionMessage2 + "" + exceptionMessage3, Toast.LENGTH_LONG).show();

a = (t1 + t2 + t3)/3;
result = m / a;

}



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

Button gcc_button = (Button) findViewById(R.id.gcc_button);
gcc_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

TextView gcc_result_textView = (TextView) findViewById(R.id.g_result_textView);
gcc_result_textView.setText(result + " ");

}
});
}
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".G_Inflow_Rate">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:text="Tiempo (en segundos)"
app:layout_constraintBottom_toTopOf="@+id/t1editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/t2editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:ems="10"
android:hint="tiempo 2 segundos"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/t3editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/t1editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:ems="10"
android:hint="tiempo 1 segundos"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/t2editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/medidoreditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="40dp"
android:ems="10"
android:hint="litros"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/t3editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="48dp"
android:ems="10"
android:hint="tiempo 3 segundos"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/gcc_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="24dp"
android:text="Litros del balde/botella"
app:layout_constraintBottom_toTopOf="@+id/medidoreditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/gcc_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="64dp"
android:text="Calcular"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/g_result_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="60dp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />

<Button
android:id="@+id/button8"
android:layout_width="423dp"
android:layout_height="88dp"
android:text="info"
tools:layout_editor_absoluteX="-6dp"
tools:layout_editor_absoluteY="-8dp" />
</android.support.constraint.ConstraintLayout>

我的代码如下,此时我很喜欢一些关于如何继续和构建它的指导,因为我 100% 确定我构建的代码非常错误。

非常感谢!

最佳答案

您可以避免 try-catch 仅检查 EditText 数据是否为空,然后将其转换为 Integer。我已经发布了一个EditText。你可以为别人做

String editor =  medidoreditText.getText().toString();
m = TextUtils.isEmpty(editor) ? 0 : Integer.parseInt(editor);

关于java - 尝试使用多个 EditText 进行 Catch,并与 OnClick 进行交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58776189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com