结构:
主活动xml代码:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字一" />
<EditText
android:id="@+id/editText_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please input one"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字二" />
<EditText
android:id="@+id/editText_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please input two"
android:inputType="number"
/>
</LinearLayout>
<TextView
android:id="@+id/num_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Result is :" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="Add" />
<Button
android:id="@+id/history"
android:text="历史"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
历史活动代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".HistoryActivity">
<ListView
android:id="@+id/id_listview"
android:layout_width="395dp"
android:layout_height="715dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
主活动.java
package com.example.thefirst;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText editText_one;
private EditText editText_two;
private TextView textView_result;
private Button btn_add;
private Button btn_his;
private ArrayList<String> res_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
editText_one = findViewById(R.id.editText_one);
editText_two = findViewById(R.id.editText_two);
textView_result = findViewById(R.id.num_result);
res_list = new ArrayList<>();
btn_add = findViewById(R.id.add);
btn_his = findViewById(R.id.history);
btn_his.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
//intent.putStringArrayListExtra("history",res_list);
startActivity(intent);
}
});
}
public void add(View v){
int add_one = Integer.valueOf(editText_one.getText().toString());
int add_two = Integer.valueOf(editText_two.getText().toString());
int result = add_one+add_two;
res_list.add(add_one+" + "+add_two+" = "+result);
textView_result.setText("Result is:"+result);
saveHistory();
}
public void saveHistory(){
SharedPreferences.Editor editor = getSharedPreferences("historyDataList", MODE_PRIVATE).edit();
editor.putInt("historyNum",res_list.size());
for(int i=0;i<res_list.size();i++){
editor.putString("item_"+i,res_list.get(i));
}
editor.apply();
// editor.commit();
}
}
历史活动.java
package com.example.thefirst;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class HistoryActivity extends AppCompatActivity {
private ArrayList<String> history = new ArrayList<>();
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView = findViewById(R.id.id_listview);
SharedPreferences pref = getSharedPreferences("historyDataList", MODE_PRIVATE);
int size = pref.getInt("historyNum",0);
for(int i=0;i<size;i++){
history.add(pref.getString("item_"+i,""));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, history);
listView.setAdapter(adapter);
}
}