• Android 开发笔记___Application操作全局变量


    只要app在运行中,他就是一个application。因此可以用它来保存一些全局变量

     1 package com.example.alimjan.hello_world;
     2 
     3 import android.app.Application;
     4 
     5 import java.util.HashMap;
     6 
     7 /**
     8  * Created by alimjan on 7/5/2017.
     9  */
    10 
    11         import android.graphics.Bitmap;
    12         import android.util.Log;
    13 
    14 
    15 public class MainApplication extends Application {
    16 
    17     private final static String TAG = "MainApplication";
    18     private static MainApplication mApp;
    19     public HashMap<String, String> mInfoMap = new HashMap<String, String>();
    20 
    21     public static MainApplication getInstance() {
    22         return mApp;
    23     }
    24 
    25     @Override
    26     public void onCreate() {
    27         super.onCreate();
    28         mApp = this;
    29         Log.d(TAG, "onCreate");
    30     }
    31 
    32     @Override
    33     public void onTerminate() {
    34         Log.d(TAG, "onTerminate");
    35         super.onTerminate();
    36     }
    37 
    38     public HashMap<Long, Bitmap> mIconMap = new HashMap<Long, Bitmap>();
    39 
    40 
    41 
    42 }

    write

      1 package com.example.alimjan.hello_world;
      2 
      3 /**
      4  * Created by alimjan on 7/5/2017.
      5  */
      6 
      7 
      8         import android.content.Context;
      9         import android.content.Intent;
     10         import android.os.Bundle;
     11         import android.support.v7.app.AppCompatActivity;
     12         import android.view.View;
     13         import android.view.View.OnClickListener;
     14         import android.widget.AdapterView;
     15         import android.widget.ArrayAdapter;
     16         import android.widget.EditText;
     17         import android.widget.Spinner;
     18         import android.widget.Toast;
     19         import android.widget.AdapterView.OnItemSelectedListener;
     20         import com.example.alimjan.hello_world.Utils.DateUtil;
     21 
     22 
     23 public class class_4_4_2 extends AppCompatActivity implements OnClickListener {
     24 
     25     private EditText et_name;
     26     private EditText et_age;
     27     private EditText et_height;
     28     private EditText et_weight;
     29     private boolean bMarried = false;
     30 
     31     @Override
     32     protected void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34         setContentView(R.layout.code_4_4_2);
     35         et_name = (EditText) findViewById(R.id.et_name);
     36         et_age = (EditText) findViewById(R.id.et_age);
     37         et_height = (EditText) findViewById(R.id.et_height);
     38         et_weight = (EditText) findViewById(R.id.et_weight);
     39         findViewById(R.id.btn_save).setOnClickListener(this);
     40 
     41         ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
     42                 R.layout.item_select, typeArray);
     43         typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
     44         Spinner sp_married = (Spinner) findViewById(R.id.sp_married);
     45         sp_married.setPrompt("请选择婚姻状况");
     46         sp_married.setAdapter(typeAdapter);
     47         sp_married.setSelection(0);
     48         sp_married.setOnItemSelectedListener(new TypeSelectedListener());
     49     }
     50 
     51     private String[] typeArray = {"未婚", "已婚"};
     52     class TypeSelectedListener implements OnItemSelectedListener {
     53         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
     54             bMarried = (arg2==0)?false:true;
     55         }
     56 
     57         public void onNothingSelected(AdapterView<?> arg0) {
     58         }
     59     }
     60 
     61     @Override
     62     public void onClick(View v) {
     63         if (v.getId() == R.id.btn_save) {
     64             String name = et_name.getText().toString();
     65             String age = et_age.getText().toString();
     66             String height = et_height.getText().toString();
     67             String weight = et_weight.getText().toString();
     68             if (name==null || name.length()<=0) {
     69                 showToast("请先填写姓名");
     70                 return;
     71             }
     72             if (age==null || age.length()<=0) {
     73                 showToast("请先填写年龄");
     74                 return;
     75             }
     76             if (height==null || height.length()<=0) {
     77                 showToast("请先填写身高");
     78                 return;
     79             }
     80             if (weight==null || weight.length()<=0) {
     81                 showToast("请先填写体重");
     82                 return;
     83             }
     84 
     85             MainApplication app = MainApplication.getInstance();
     86             app.mInfoMap.put("name", name);
     87             app.mInfoMap.put("age", age);
     88             app.mInfoMap.put("height", height);
     89             app.mInfoMap.put("weight", weight);
     90             app.mInfoMap.put("married", typeArray[bMarried==false?0:1]);
     91             app.mInfoMap.put("update_time", DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss"));
     92             showToast("数据已写入全局内存");
     93         }
     94     }
     95 
     96     private void showToast(String desc) {
     97         Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
     98     }
     99 
    100     public static void startHome(Context mcontext){
    101         Intent intent = new Intent(mcontext,class_4_4_2.class);
    102         mcontext.startActivity(intent);
    103     }
    104 
    105 }
      1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      2     android:layout_width="match_parent"
      3     android:layout_height="match_parent"
      4     android:focusable="true"
      5     android:focusableInTouchMode="true"
      6     android:orientation="vertical"
      7     android:padding="10dp" >
      8 
      9     <RelativeLayout
     10         android:layout_width="match_parent"
     11         android:layout_height="50dp" >
     12 
     13         <TextView
     14             android:id="@+id/tv_name"
     15             android:layout_width="wrap_content"
     16             android:layout_height="match_parent"
     17             android:layout_alignParentLeft="true"
     18             android:gravity="center"
     19             android:text="姓名:"
     20             android:textColor="@color/black"
     21             android:textSize="17sp" />
     22 
     23         <EditText
     24             android:id="@+id/et_name"
     25             android:layout_width="match_parent"
     26             android:layout_height="match_parent"
     27             android:layout_marginBottom="5dp"
     28             android:layout_marginTop="5dp"
     29             android:layout_toRightOf="@+id/tv_name"
     30             android:background="@drawable/editext_selector"
     31             android:gravity="left|center"
     32             android:hint="请输入姓名"
     33             android:inputType="text"
     34             android:maxLength="12"
     35             android:textColor="@color/black"
     36             android:textColorHint="@color/grey"
     37             android:textCursorDrawable="@drawable/text_cursor"
     38             android:textSize="17sp" />
     39     </RelativeLayout>
     40 
     41     <RelativeLayout
     42         android:layout_width="match_parent"
     43         android:layout_height="50dp" >
     44 
     45         <TextView
     46             android:id="@+id/tv_age"
     47             android:layout_width="wrap_content"
     48             android:layout_height="match_parent"
     49             android:layout_alignParentLeft="true"
     50             android:gravity="center"
     51             android:text="年龄:"
     52             android:textColor="@color/black"
     53             android:textSize="17sp" />
     54 
     55         <EditText
     56             android:id="@+id/et_age"
     57             android:layout_width="match_parent"
     58             android:layout_height="match_parent"
     59             android:layout_marginBottom="5dp"
     60             android:layout_marginTop="5dp"
     61             android:layout_toRightOf="@+id/tv_age"
     62             android:background="@drawable/editext_selector"
     63             android:gravity="left|center"
     64             android:hint="请输入年龄"
     65             android:inputType="number"
     66             android:maxLength="2"
     67             android:textColor="@color/black"
     68             android:textColorHint="@color/grey"
     69             android:textCursorDrawable="@drawable/text_cursor"
     70             android:textSize="17sp" />
     71     </RelativeLayout>
     72 
     73     <RelativeLayout
     74         android:layout_width="match_parent"
     75         android:layout_height="50dp" >
     76 
     77         <TextView
     78             android:id="@+id/tv_height"
     79             android:layout_width="wrap_content"
     80             android:layout_height="match_parent"
     81             android:layout_alignParentLeft="true"
     82             android:gravity="center"
     83             android:text="身高:"
     84             android:textColor="@color/black"
     85             android:textSize="17sp" />
     86 
     87         <EditText
     88             android:id="@+id/et_height"
     89             android:layout_width="match_parent"
     90             android:layout_height="match_parent"
     91             android:layout_marginBottom="5dp"
     92             android:layout_marginTop="5dp"
     93             android:layout_toRightOf="@+id/tv_height"
     94             android:background="@drawable/editext_selector"
     95             android:gravity="left|center"
     96             android:hint="请输入身高"
     97             android:inputType="number"
     98             android:maxLength="3"
     99             android:textColor="@color/black"
    100             android:textColorHint="@color/grey"
    101             android:textCursorDrawable="@drawable/text_cursor"
    102             android:textSize="17sp" />
    103     </RelativeLayout>
    104 
    105     <RelativeLayout
    106         android:layout_width="match_parent"
    107         android:layout_height="50dp" >
    108 
    109         <TextView
    110             android:id="@+id/tv_weight"
    111             android:layout_width="wrap_content"
    112             android:layout_height="match_parent"
    113             android:layout_alignParentLeft="true"
    114             android:gravity="center"
    115             android:text="体重:"
    116             android:textColor="@color/black"
    117             android:textSize="17sp" />
    118 
    119         <EditText
    120             android:id="@+id/et_weight"
    121             android:layout_width="match_parent"
    122             android:layout_height="match_parent"
    123             android:layout_marginBottom="5dp"
    124             android:layout_marginTop="5dp"
    125             android:layout_toRightOf="@+id/tv_weight"
    126             android:background="@drawable/editext_selector"
    127             android:gravity="left|center"
    128             android:hint="请输入体重"
    129             android:inputType="numberDecimal"
    130             android:maxLength="5"
    131             android:textColor="@color/black"
    132             android:textColorHint="@color/grey"
    133             android:textCursorDrawable="@drawable/text_cursor"
    134             android:textSize="17sp" />
    135     </RelativeLayout>
    136 
    137     <RelativeLayout
    138         android:layout_width="match_parent"
    139         android:layout_height="50dp" >
    140 
    141         <TextView
    142             android:id="@+id/tv_married"
    143             android:layout_width="wrap_content"
    144             android:layout_height="match_parent"
    145             android:layout_alignParentLeft="true"
    146             android:gravity="center"
    147             android:text="婚否:"
    148             android:textColor="@color/black"
    149             android:textSize="17sp" />
    150 
    151         <Spinner
    152             android:id="@+id/sp_married"
    153             android:layout_width="match_parent"
    154             android:layout_height="match_parent"
    155             android:layout_toRightOf="@+id/tv_married"
    156             android:gravity="left|center"
    157             android:spinnerMode="dialog" />
    158     </RelativeLayout>
    159 
    160     <Button
    161         android:id="@+id/btn_save"
    162         android:layout_width="match_parent"
    163         android:layout_height="wrap_content"
    164         android:text="保存到全局内存"
    165         android:textColor="@color/black"
    166         android:textSize="20sp" />
    167 
    168 </LinearLayout>

    read

     1 package com.example.alimjan.hello_world;
     2 
     3 import java.util.Map;
     4 
     5 /**
     6  * Created by alimjan on 7/5/2017.
     7  */
     8 
     9 
    10 import android.content.Context;
    11 import android.content.Intent;
    12 import android.os.Bundle;
    13         import android.support.v7.app.AppCompatActivity;
    14         import android.widget.TextView;
    15 
    16 
    17 public class class_4_4_2_1 extends AppCompatActivity {
    18 
    19     private TextView tv_app;
    20 
    21     @Override
    22     protected void onCreate(Bundle savedInstanceState) {
    23         super.onCreate(savedInstanceState);
    24         setContentView(R.layout.code_4_4_2_1);
    25         tv_app = (TextView) findViewById(R.id.tv_app);
    26         readAppMemory();
    27     }
    28 
    29     private void readAppMemory() {
    30         String desc = "全局内存中保存的信息如下:";
    31         MainApplication app = MainApplication.getInstance();
    32         Map<String, String> mapParam = app.mInfoMap;
    33         for (Map.Entry<String, String> item_map : mapParam.entrySet()) {
    34             desc = String.format("%s
     %s的取值为%s",
    35                     desc, item_map.getKey(), item_map.getValue());
    36         }
    37         if (mapParam==null || mapParam.size()<=0) {
    38             desc = "全局内存中保存的信息为空";
    39         }
    40         tv_app.setText(desc);
    41     }
    42 
    43     public static void startHome(Context mcontext){
    44         Intent intent = new Intent(mcontext,class_4_4_2_1.class);
    45         mcontext.startActivity(intent);
    46     }
    47 
    48 }
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:focusable="true"
     5     android:focusableInTouchMode="true"
     6     android:orientation="vertical"
     7     android:padding="10dp" >
     8 
     9     <TextView
    10         android:id="@+id/tv_app"
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content"
    13         android:textColor="@color/black"
    14         android:textSize="17sp" />
    15 
    16 </LinearLayout>
  • 相关阅读:
    LINUX 新手 入门 教程
    mysql 新手入门 官方文档+官方中文文档附地址
    Git常用命令
    修改dotnet运行.netcore web程序默认端口
    centos7.6下部署.netcore3.1web程序
    supervisor安装及使用(转载)
    linux(CentOS7.6)下安装mysql8.0并使用navicat远程访问
    uni-app为组件uni-icons增加自定义图标(超简单)
    常用正则表达式收录(持续更新)
    CentOS7.6安装SQL SERVER 2017
  • 原文地址:https://www.cnblogs.com/alimjan/p/7122905.html
Copyright © 2020-2023  润新知