• exam02-1



    activity_main.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:layout_margin="10dp"
        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="Number One" />
    
            <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="Number One" />
            <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="vertical">
    
            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:onClick="add"
                android:text="Add" />
    
        </LinearLayout>
    
    </LinearLayout>
    
    

    activity_history.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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" />
    </android.support.constraint.ConstraintLayout>
    

    MainActivity.xml

    package com.example.exam02_1;
    
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    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;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private EditText editText_one;
        private EditText editText_two;
        private TextView textView_result;
        //private Button btn_add;
        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);
        }
    
        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);
        }
    
        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();
        }
        //显示History菜单
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_main,menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case R.id.history:
                    saveHistory();
                    Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
                    //intent.putStringArrayListExtra("history",res_list);
                    startActivity(intent);
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
    }
    
    

    HistoryActivity.xml

    package com.example.exam02_1;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    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);
        }
    }
    
    
  • 相关阅读:
    HDU 2888 Check Corners (模板题)【二维RMQ】
    POJ 3264 Balanced Lineup(模板题)【RMQ】
    poj 3368 Frequent values(经典)【RMQ】
    SPOJ RPLN (模板题)(ST算法)【RMQ】
    UVA 796 Critical Links(模板题)(无向图求桥)
    UVA 315 Network (模板题)(无向图求割点)
    POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】
    poj 3067 Japan 【树状数组】
    POJ 2481 Cows 【树状数组】
    POJ 1195 Mobile phones【二维树状数组】
  • 原文地址:https://www.cnblogs.com/lyszyl/p/10816544.html
Copyright © 2020-2022  润新知