简体   繁体   English

我如何使用 if else 语句在 android 工作室的 EditText 中输入数字

[英]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.

相关问题 Android Studio 如何使用字符串字符数创建edittext? - Android Studio How can I create edittext with number of string characters? 如何将 if 语句放在 if-else if 语句中 - How can I put an if statement inside of a if-else if statement 如何借助android studio中的按钮拨打edittext中给出的电话号码? - How can I call the phone number given in the edittext with the help of a button in android studio? 在 android studio 中,我如何将 EditText 字段中的输入作为数字输入,例如; 1452 并将其转换为整数数组{1,4,5,2} - In android studio how can i take input in EditText field as a number eg; 1452 and convert it into integer array{1,4,5,2} 如何存储和 if else 语句的结果以在 android 应用程序的另一页上使用 - How can I store the result of and if else statement to use on another page of an android app 我如何将 textview 放在 edittext 中 - How can i put textview inside edittext 如何在if-else语句中使用字符串? - How can I use a string out of and if-else statement? 如何在谓词中使用 if/else 语句? - How can I use an if/else-statement within predicates? 如何在Android EditText中使用土耳其语字符? - How can I use Turkish characters in Android EditText? 如何在Android中对单选按钮使用if else语句 - how to use if else statement for radiobuttons in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM