【发布时间】: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>=1 && input<=20) { result.setText("first result"); } else (input>=21 && input<=30){ result.setText("second result"); }时它给出错误@
标签: android android-layout android-studio