11package com.justaloli.masscalc
22
3+ import android.content.Context
4+ import android.content.SharedPreferences
35import android.graphics.Color
46import android.os.Bundle
7+ import android.view.View
58import android.view.inputmethod.EditorInfo
69import android.widget.ArrayAdapter
10+ import android.widget.ScrollView
711import androidx.appcompat.app.AppCompatActivity
812import kotlinx.android.synthetic.main.activity_main.*
913
@@ -41,11 +45,21 @@ class MainActivity : AppCompatActivity() {
4145 textView.text = " " ;true // 这个true是这个长按监听需要的一个值,没啥用,就true吧
4246 }// 设置长按清屏
4347
48+ // 设置scrollview自动滚到底
49+ scrollView.viewTreeObserver.addOnGlobalLayoutListener {
50+ scrollView.post(){
51+ kotlin.run { scrollView.fullScroll(View .FOCUS_DOWN )}
52+ }
53+ }
4454 }
4555 private fun processInput (){ // 处理
4656 val input = editText.text.toString() // 获取输入框的输入
4757 val mass = calcMain(input) // 调用计算函数
48- myprint(" $input : $mass " ) // 输出结果
58+ if (mass!= 0.0 ) {
59+ val massStr = String .format(" %.3f" , mass)
60+ myprint(" $input : $massStr " ) // 输出结果
61+ ShareUtil .putString(input,mass.toString(),applicationContext)
62+ }
4963 }
5064
5165 // 两个用于相对分子质量的函数
@@ -58,15 +72,17 @@ class MainActivity : AppCompatActivity() {
5872 if (relist[1 ]== " " ){
5973 relist[1 ] = " 1"
6074 }
61- mm[relist[0 ]]?.also { r+ = it* relist[1 ].toInt()}
75+ val m = mm.getOrDefault(relist[0 ],0.0 )
76+ if (m== 0.0 ){return 0.0 }
77+ r+ = m* (relist[1 ].toInt())
6278 }
6379 return r
6480 }
6581 private fun calcMain (input : String ):Double { // 主要用于识别、拆分输入中的括号,并累加全部计算结果
6682 mm[input]?.also { return it }
6783 var res = 0.0
6884 var allMatch = " "
69- Regex (" \\ ([^()]*(((\\ ()[^()]*)+((\\ ))[^()]*)+)*\\ )[0-9]*|[A-Z][a-z]* [0-9]*" ).findAll(input).forEach {
85+ Regex (" \\ ([^()]*(((\\ ()[^()]*)+((\\ ))[^()]*)+)*\\ )[0-9]*|[A-Z][a-z]? [0-9]*" ).findAll(input).forEach {
7086 val i = it.value
7187 allMatch+ = i
7288 if (i!= " " ){
@@ -83,11 +99,16 @@ class MainActivity : AppCompatActivity() {
8399 else {
84100 r = calcMass(arrayOf(i))
85101 }
102+ if (r== 0.0 ){
103+ myprint(" input incorrect. input: $input " )
104+ return 0.0
105+ }
86106 res+ = r
87107 }
88108 }
109+ // if(allMatch!=input){
89110 if (allMatch!= input){
90- myprint(" input incorrect. matched: $allMatch ; input:$input " )
111+ myprint(" input incorrect. input: $input " )
91112 res = 0.0
92113 }
93114 // 写入历史部分 目前停用
@@ -110,10 +131,35 @@ class MainActivity : AppCompatActivity() {
110131// historyList+=Pair(fn,mass)
111132// }
112133// mm.putAll(historyList)
134+ }
135+ object ShareUtil{
136+ var sps: SharedPreferences ? = null
137+ fun getSps (context : Context ): SharedPreferences {
138+ if (sps== null ){
139+ sps = context.getSharedPreferences(" default" , Context .MODE_PRIVATE )
140+ }
141+ return sps!!
142+ }
143+ fun putString (key : String ,value : String? ,context : Context ){
144+ if (! value.isNullOrBlank()){
145+ val editor: SharedPreferences .Editor = getSps(context).edit()
146+ editor.putString(key,value)
147+ editor.apply ()
148+ }
149+ }
150+ fun getString (key : String ,context : Context ):String? {
151+ if (! key.isBlank()){
152+ val sps = getSps(context)
153+ return sps.getString(key,null )
154+ }
155+ return null
156+ }
157+
113158 }
114159 // 打印输出结果函数
115160 private fun myprint (text : String , isclear : Boolean =false){
116161 if (isclear){textView.text= " " }
117- textView.append(" \n output:$text \n " )
162+ textView.append(" \n $text \n " )
163+
118164 }
119165}
0 commit comments