java - 如何在 Android Studio CalcApp 中添加 TIMESTAMP?
问题描述
↓根据下面的链接。 简单计算器的历史 - Android
timestamp
单击“=”按钮时尝试添加一个。我不确定这是否正常工作?
我的最终目标是,
・当用户单击“=”按钮时,将计算历史保存到History
数组中。TIMESTAMP
MainActivity.java
package com.example.kaguya;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.lang.String;
import android.os.Bundle;
public class MainActivity<format> extends AppCompatActivity {
Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bplus, bminus, bmulti, bdiv, beq, bc, hh;
List<String> history= new ArrayList<>();
String timestamp = new String(String.valueOf(System.currentTimeMillis()));
EditText edit1;
TextView hist;
int nr1, nr2;
boolean plus, minus, multi, div;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 = (Button) findViewById(R.id.b0);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
hh = (Button) findViewById(R.id.hh);
bplus = (Button) findViewById(R.id.bplus);
bminus = (Button) findViewById(R.id.bminus);
bmulti = (Button) findViewById(R.id.bmulti);
bdiv = (Button) findViewById(R.id.bdiv);
bc = (Button) findViewById(R.id.bc);
beq = (Button) findViewById(R.id.beq);
edit1 = (EditText) findViewById(R.id.edit1);
hist = (TextView) findViewById(R.id.hist);
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "0");
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "4");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText(edit1.getText() + "9");
}
});
bplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edit1 == null){
edit1.setText("");
}else {
nr1 = Integer.parseInt(edit1.getText() + "");
plus= true;
edit1.setText(null);
}
}
});
bminus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nr1 = Integer.parseInt(edit1.getText() + "");
minus = true ;
edit1.setText(null);
}
});
bmulti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nr1 = Integer.parseInt(edit1.getText() + "");
multi = true ;
edit1.setText(null);
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nr1 = Integer.parseInt(edit1.getText()+"");
div = true ;
edit1.setText(null);
}
});
bc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1.setText("");
}
});
beq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nr2 = Integer.parseInt(edit1.getText() + "");
double res = 0;
String res2 = timestamp;
if (plus == true){
res = nr1 + nr2;
res2 = timestamp;
edit1.setText(nr1 + nr2+"");
plus=false;
}
if (minus == true){
res = nr1 - nr2;
res2 = timestamp;
edit1.setText(nr1 - nr2+"");
minus=false;
}
if (multi == true){
res = nr1 * nr2;
res2 = timestamp;
edit1.setText(nr1 * nr2+"");
multi=false;
}
if (div == true){
res = nr1 / nr2;
res2 = timestamp;
edit1.setText(nr1 / nr2+"");
div=false;
}
history.add(String.valueOf(res));
history.add(String.valueOf(res2));
}
});
hh.setOnClickListener(new View.OnClickListener() {
int index = 0;
public void onClick(View v) {
history.get(index);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:id="@+id/hist"
android:layout_height="100dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit1" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:id="@+id/GridLayout">
<Button
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b5"
android:layout_row="1"
android:layout_column="1"/>
<Button
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b6"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bminus" />
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b7"
android:layout_row="2"
android:layout_column="0"/>
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b8"
android:layout_row="2"
android:layout_column="1" />
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b9"
android:layout_row="2"
android:layout_column="2" />
<Button
android:text="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bmulti"
android:layout_row="2"
android:layout_column="3" />
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bplus"
android:layout_row="0"
android:layout_column="3" />
<Button
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:layout_row="0"
android:layout_column="2" />
<Button
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:layout_row="1"
android:layout_column="0" />
<Button
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b0"
android:layout_row="3"
android:layout_column="1" />
<Button
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:layout_row="0"
android:layout_column="1" />
<Button
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:layout_row="0"
android:layout_column="0" />
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bdiv"
android:layout_row="3"
android:layout_column="3" />
<Button
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/beq"
android:layout_row="3"
android:layout_column="2" />
<Button
android:text="C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bc"
android:layout_row="3"
android:layout_column="0" />
<Button
android:id="@+id/bc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:text="C" />
<Button
android:id="@+id/hh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:text="History" />
</GridLayout>
</LinearLayout>
解决方案
如果要存储历史记录,可以使用数据库(推荐)。
如果托管是临时的,则可以使用共享首选项。
一些数据库供您参考:
方镁石
领域
房间
存储时间戳时,存储时可以将其视为 Integer(或 Long)或 String。
要获取当前时间戳,可以使用此命令。
Long tsLong = System.currentTimeMillis () / 1000;
String ts = tsLong.toString ();
推荐阅读
- python - 如何计算 Python 列表中出现的次数?
- javascript - 停止或反转单击的 Vuetify v-tab 项目的突出显示
- python - 通过 sqlalchemy 写入操作的 sqlite3 在 USB 驱动器上很慢
- spring-boot - 多 JPA 事务内一种方法
- amazon-web-services - Helm 创建的 Kubernetes 命名空间和资源不会删除
- c++ - 错误:未在此范围内声明“standardToMilitary”
- mysql - union all 同一张表需要显示不同的数据
- java - 我的 App Engine 前端实例使用情况如何?
- reactjs - 将状态添加到“推送”反应路由器?
- jsonata - Jsonata,合并对象数组