【问题标题】:How do i implement multiple conditions in android studio我如何在android studio中实现多个条件
【发布时间】:2016-09-22 20:00:57
【问题描述】:

我正在尝试构建一个计费应用程序,其中单位从 1 到 1000 或以上,有一定条件来计算要支付的总金额。如果客户消费 1 到 20 个单位,他将被收取卢比。 30(最低收费)+ 卢比。每单位 3,意味着如果他消费 12 单位,他必须支付卢比 (30+12*3)= 66 卢比,还有另一个条件,如果他消费超过 20 单位,最低费用将为卢比。 110 元,每单位费用为 7 卢比。现在,如果消费者消费 25 单位,那么他必须支付卢比 (110+(25-20)*7)= 145 卢比。我完全不知道从哪里开始。我已经制作了一个布局文件,以及用于此的 java 文件。如果有人帮助我,我将不胜感激。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:textAlignment="center"
        android:id="@+id/textView"
        android:text="@string/appIntro"/>

</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="16sp"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="2dp"
        android:text="@string/noOfUnitsMsg"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginEnd="2dp"
        android:layout_marginStart="1dp"
        android:digits="0123456789"
        android:inputType="number"
        android:id="@+id/input"
        android:textSize="16sp"
        android:maxLength="3"
        android:hint="@string/input"/>

    </LinearLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:id="@+id/calculate"
         />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/resultMsg"
        android:textAlignment="center"
        android:layout_weight="1"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/result"
        android:textAlignment="center"
        android:textSize="24sp"
        android:layout_weight="1"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="100dp"
        android:id="@+id/result"
        />
    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="1dp"
            android:layout_marginStart="2dp"
            android:textSize="16sp"
            android:text="@string/clear"
            tools:ignore="ButtonStyle" />

        <Button
            android:id="@+id/credits"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="1dp"
            android:layout_marginStart="1dp"
            android:textSize="16sp"
            android:text="@string/credits" />

        <Button
            android:id="@+id/exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="1dp"
            android:layout_weight="1"
            android:textSize="16sp"
            android:text="@string/exit" />

    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.android.nepalelectricity;

import android.content.Intent;
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;

public class MainActivity extends AppCompatActivity {

    EditText input;
    Button calculate;
    Button clear;
    Button credits;
    Button exit;
    TextView result;


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

        input=new EditText(this);
        calculate = new Button(this);
        result=new TextView(this);
        clear=new Button(this);
        credits = new Button(this);
        exit = new Button(this);

        input=(EditText)findViewById(R.id.input);
        credits = (Button) findViewById(R.id.credits);
        clear = (Button) findViewById(R.id.clear);
        exit = (Button) findViewById(R.id.exit);
        calculate = (Button) findViewById(R.id.calculate);
        result=(TextView)findViewById(R.id.result);

        calculate.setOnClickListener(new View.OnClickListener() {
            // Perform action on Calculate Button click
            @Override
            public void onClick(View v) {

               //  i am confused in this section
               /* if (input.getText().length()==0){
                    result.setText(R.string.err_msg);
                }
                else {
                    result.setText("You are great");
                    //change the integer value into string
                   result.setText("");
                    int input1 = Integer.parseInt(input.getText().toString());


                }  */
                //Till here

            }
        });


        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input.setText("");
                result.setText("");
            }
        });
        credits.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Credits.class));
            }
        });



        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "Thank you for using app!", Toast.LENGTH_SHORT).show();
                finish();

            }
        });
    }

}

【问题讨论】:

  • 看起来你已经为你的计算按钮定义了一个onClickListener,你不能把上面描述的逻辑放在那里吗?
  • 当我把这个逻辑放在if (input.getText().length()==0){ result.setText(R.string.err_msg); } else if (input&gt;=1 &amp;&amp; input&lt;=20) { result.setText("first result"); } else (input&gt;=21 &amp;&amp; input&lt;=30){ result.setText("second result"); }时它给出错误@

标签: android android-layout android-studio


【解决方案1】:

使用如下逻辑:

calculate.setOnClickListener(new View.OnClickListener() {
   // Perform action on Calculate Button click
   @Override
   public void onClick(View v) {
        String value = input.getText().toString(); // if error occur here try this : String.valueOf(input.getText().toString());
        if(TextUtils.isEmpty(value)){
           //make toast saying value is null/empty
        }
        else{
          final int inputValue = Integer.parseInt(value);  

          if(inputValue > 1 && inputValue <= 20){
          result.setText(Integer.toString((inputValue*3)+30)); //try avoiding brackets in calculation if error occur, computer does not understand BODMAS calculation format.
          }
          else if(inputvalue > 20){
          result.setText(Integer.toString(((inputValue-20)*7)+110)); //try avoiding brackets in calculation if error occur, computer does not understand BODMAS calculation format.
          }
          else{
               // make toast saying value not in range
          }
       }
      }
    });

【讨论】:

  • 我的荣幸。好好学习android,继续写代码。
  • 如果我想' else if(inputvalue >=50) {result.setText(Integer.toString(((inputValue-20)*8.5)+205));'我收到关于双精度字符串转换的错误。
  • 使用result.setText(String.valueOf(((inputValue-20)*8.5)+205));
  • 谢谢@W4R10CK,我收到关于“运算符'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多