• 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);
        }
    }
    
    
  • 相关阅读:
    SQL调用另一台服务器的表及存储过程(SQL函数openrowset()的使用以及相关问题处理)
    解决SQL Server 阻止了对组件Ad Hoc Distributed Queries访问的方法
    sql server exec 参数的传递
    c#修改webservice 的地址和端口(修改配置文件)
    JS转换HTML转义符,编码及解码
    nginx使用ssl模块配置支持HTTPS访问,腾讯云申请免费证书
    thinkcmf 相关
    ThinkJS 开发node后端 使用 简介
    linux复制指定目录下的全部文件到另一个目录中
    谷歌地图,国内使用Google Maps JavaScript API,国外业务
  • 原文地址:https://www.cnblogs.com/lyszyl/p/10816544.html
Copyright © 2020-2023  润新知