[英]how can i use if else statement to put a number in the EditText in android studio
im new to android, but im developing an application that has conditions in the background.i want if some one inputs the AVB number in the first EditText, the second one should be automatically filled from the ranges in the if statement and then multiplies and puts the answer in the tfeed TextView. please help me out as soon as possible.我是 android 的新手,但我正在开发一个在后台有条件的应用程序。我想如果有人在第一个 EditText 中输入 AVB 编号,第二个应该自动从 if 语句中的范围填充,然后乘以并放置tfeed TextView 中的答案。请尽快帮助我。 Here is my fish_feed.java class
这是我的 fish_feed.java class
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Fish_Feed extends AppCompatActivity {
EditText number1;
EditText number2;
TextView total_feed;
Button calc;
double num1,num2,product ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fish_feed);
Toolbar toolbar = findViewById(R.id.feed_fry);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Calculate Feed Per Fish in a day");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
number1 = (EditText)findViewById(R.id.abw);
number2 = (EditText)findViewById(R.id.fish);
total_feed = (TextView) findViewById(R.id.tfeed);
calc = (Button) findViewById(R.id.feed);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
product = num1*(num2/100) ;
if (num1 < 2){
number2.setText((int) 16.5) ;
}
else if(num1 >=2 && num1<=10){
number2.setText((int) 7) ;
}
else if(num1 >=10 && num1<=100){
number2.setText((int) 3) ;
}
else if(num1 >=100 && num1<=500){
number2.setText((int) 1.5) ;
}
else if(num1 >500){
number2.setText((int) 1) ;
}
total_feed.setText("Number of fish to be stocked is " + product);
}
});
}
}
**And the activity_fish_feed**
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Fish_Feed">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/feed_fry"
app:titleTextColor="#fff"
android:background="#FF018786"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Average Body Weight of Your Fish"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginTop="15dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<EditText
android:id="@+id/abw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="Enter your ABW"
/>
<EditText
android:id="@+id/fish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginTop="15dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<Button
android:id="@+id/feed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:backgroundTint="#FF018786"
android:text="CALCULATE TOTAL FEED"
android:layout_marginLeft="30dp"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="@+id/yield"
app:layout_constraintTop_toBottomOf="@+id/yield" />
<TextView
android:id="@+id/tfeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
</LinearLayout>
</ScrollView>
Try this once -试试这个 -
XML File- XML 档案-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/feed_fry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF018786"
app:titleTextColor="#fff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="16dp"
android:text="Enter Average Body Weight of Your Fish"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/abw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="Enter your ABW"
android:inputType="number" />
<EditText
android:id="@+id/fish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="16dp"
android:enabled="false"
android:textColor="#000"
android:textSize="20sp" />
<Button
android:id="@+id/feed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="16dp"
android:backgroundTint="#FF018786"
android:text="CALCULATE TOTAL FEED"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="@+id/yield"
app:layout_constraintTop_toBottomOf="@+id/yield" />
<TextView
android:id="@+id/tfeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
Activity File活动文件
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity2 extends AppCompatActivity {
EditText number1;
EditText number2;
TextView total_feed;
Button calc;
double product;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = findViewById(R.id.feed_fry);
toolbar.setTitle("Calculate Feed Per Fish in a day");
number1 = findViewById(R.id.abw);
number2 = findViewById(R.id.fish);
total_feed = findViewById(R.id.tfeed);
number1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() == 0) {
number2.setText("");
total_feed.setText("");
} else {
double number;
int value = Integer.parseInt(String.valueOf(s));
if (value < 2) {
number = 16.5;
} else if (value >= 2 && value <= 10) {
number = 7;
} else if (value >= 10 && value <= 100) {
number = 3;
} else if (value >= 100 && value <= 500) {
number = 1.5;
} else if (value > 500) {
number = 1;
} else {
number = 0;
}
number2.setText(String.valueOf(number));
}
}
});
calc = findViewById(R.id.feed);
calc.setOnClickListener(v -> {
String num1 = number1.getText().toString().trim();
String num2 = number2.getText().toString().trim();
if (num1.isEmpty() && num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "Add Num", Toast.LENGTH_LONG).show();
} else {
product = Double.parseDouble(num1) * (Double.parseDouble(num2) / 100);
total_feed.setText("Number of fish to be stocked is " + product);
}
});
}
}
You have to use TextChangeListener and you are setting text as int which refers to string resource ID.您必须使用 TextChangeListener 并将文本设置为引用字符串资源 ID 的 int。 You can use hardcoded string as follows
您可以使用硬编码字符串如下
number1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
num1 = Double.parseDouble(charSequence.toString());
if (num1 < 2){
number2.setText("16") ;
}
else if(num1 >=2 && num1<=10){
number2.setText("3") ;
}
else if(num1 >=10 && num1<=100){
number2.setText("3") ;
}
else if(num1 >=100 && num1<=500){
number2.setText("1") ;
}
else if(num1 >500){
number2.setText("1") ;
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.