学习目标:
- 设计一个计算存款利息的UI交互页面
学习内容:
- layout文件设计
- Java程序编写 难点处理不同按钮对应计算不同金额不同年限的不同计算方式--复杂,孤立出一个函数来
- 交互
学习产出:
1在两个按钮上放监听器,本类输入
button1.setOnClickListener(this);
2文本形式获取计算
String mystr=str. getText().toString().trim();
getText()返回的是字符串trim()去除空格
float cash=Float.parseFloat(myCash);
防止不是数字或者空的强制转化定义一下layout里面Android:inputType="number"
3弹窗提示
弹完自己消退
Toast.makeText(money.this,text:"输入不能为空",Toast.LENGTH_LONG).show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/qian"
//加了一个酷炫背景
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="存款本金:"
android:textColor="@color/white"//酷炫背景太黑了看不见字
android:textSize="30dp"
android:layout_marginTop="30dp"/>
<EditText
android:id="@+id/n1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@color/white"//酷炫背景太黑了看不见字
android:paddingBottom="10dp"
android:hint="请输入一个整数"
android:textSize="16sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="存款年限:"
android:textColor="@color/white"//酷炫背景太黑了看不见字
android:textSize="30dp"
android:layout_marginTop="30dp"/>
<EditText
android:id="@+id/n2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@color/white"//酷炫背景太黑了看不见字
android:paddingBottom="10dp"
android:hint="请输入一个整数"
android:textSize="16sp"
/>
<RelativeLayout
android:layout_marginTop="50dp"
android:layout_width="match_parent"//横向线性布局
android:layout_height="90dp">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginStart="@dimen/cardview_compat_inset_shadow"
android:layout_marginTop="@dimen/cardview_compat_inset_shadow"
android:layout_marginEnd="@dimen/cardview_compat_inset_shadow"
android:layout_marginBottom="@dimen/cardview_compat_inset_shadow"
//让按钮变得华丽
android:text="提交"
android:textColor="@color/white"
android:textSize="30dp"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/btn1"
android:layout_marginLeft="200dp"
android:layout_marginRight="-0dp"
android:text="重置"
android:textColor="@color/white"
android:textSize="30dp" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:textSize="50pt"
android:gravity="center"
android:id="@+id/textv"/>//给将来的显示内容留个位子
</LinearLayout>
欣赏一下优美流畅的走线Java程序这么写
package com.example.badbad;
import androidx.appcompat.app.AppCompatActivity;
import android.app.SearchManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class money extends AppCompatActivity {
EditText ed1,ed2;
int n;
double money;
double moneyer;
double one=0.0225;
double two=0.027;
double three=0.0324;
double five=0.036;
Button button1,button2;
TextView textv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chaptertwothree);
button1=findViewById(R.id.btn1);
button2=findViewById(R.id.btn2);
textv=findViewById(R.id.textv);
ed1=findViewById(R.id.n1);
ed2=findViewById(R.id.n2);
MyOnClickListener1 myOnClickListener1=new MyOnClickListener1();
MyOnClickListener2 myOnClickListener2=new MyOnClickListener2();
button1.setOnClickListener((View.OnClickListener)myOnClickListener1);
button2.setOnClickListener((View.OnClickListener)myOnClickListener2);
}
class MyOnClickListener1 implements View.OnClickListener{
public void onClick(View view){
String str=ed2.getText().toString();
n=Integer.parseInt(str);
str=ed1.getText().toString();
money=Double.parseDouble(str);
if(str.isEmpty()){
//提示用户不能为空
}
else if(money>0){
switch (n){
case 1:moneyer=money*(1+one);break;
case 2:moneyer=money*(1+two)*(1+two);break;
case 3:moneyer=money*(1+two)*(1+two)*(1+three);break;
case 4:moneyer=money*(1+two)*(1+two)*(1+three)*(1+three);break;
default:moneyer=money*(1+two)*(1+two)*(1+three)*(1+three);
for(int i=0;i<n-4;i++){
moneyer=moneyer*(1+five);
}
break;
}
textv.setText("本金加利息一共:" + "\n"+moneyer+"元");}
else textv.setText("非法输入。");
}
}
class MyOnClickListener2 implements View.OnClickListener{
public void onClick(View view){
textv.setText("");
ed1.setText("");
ed2.setText("");
}
}
}
提示了一种高级一点的数学计算公式
interst相当于上文中不同年份利率one two...还要用一堆if year==几的语句来规定不同的
int year=Integer.parseInt(myYear); float totalCash=Math.pow(1+interst,year)*cashNun;
当你想修改名字时候一个一个改容易漏且耽误时间,正确改法
交互
找到这个页面
以要进入temper为例
去/改为true放上进入的那段代码