Skip to content

Commit a682333

Browse files
authored
Add files via upload
1 parent 8ec4e5f commit a682333

6 files changed

Lines changed: 92 additions & 28 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
defaultConfig {
99
applicationId "com.justaloli.masscalc"
1010
minSdkVersion 26
11-
targetSdkVersion 30
11+
targetSdkVersion /**/30
1212
versionCode 1
1313
versionName "1.0"
1414

@@ -32,5 +32,5 @@ dependencies {
3232
testImplementation 'junit:junit:4.12'
3333
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
3434
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
35-
35+
/**/
3636
}

app/src/main/java/com/justaloli/masscalc/MainActivity.kt

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.justaloli.masscalc
22

3+
import android.content.Context
4+
import android.content.SharedPreferences
35
import android.graphics.Color
46
import android.os.Bundle
7+
import android.view.View
58
import android.view.inputmethod.EditorInfo
69
import android.widget.ArrayAdapter
10+
import android.widget.ScrollView
711
import androidx.appcompat.app.AppCompatActivity
812
import 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("\noutput:$text\n")
162+
textView.append("\n$text\n")
163+
118164
}
119165
}

app/src/main/res/layout/activity_main.xml

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,49 @@
3333
android:imeOptions="actionSearch"
3434
android:inputType="textNoSuggestions"
3535
android:shadowColor="@color/colorPrimary"
36+
android:textColorHint="#78909C"
3637
android:textSize="24sp"
3738
app:layout_constraintEnd_toEndOf="parent"
3839
app:layout_constraintStart_toStartOf="parent"
3940
app:layout_constraintTop_toTopOf="parent" />
4041

41-
<androidx.appcompat.widget.SwitchCompat
42-
android:id="@+id/switchHistory"
43-
android:layout_width="wrap_content"
44-
android:layout_height="wrap_content"
42+
<ScrollView
43+
android:id="@+id/scrollView"
44+
android:layout_width="403dp"
45+
android:layout_height="0dp"
4546
android:layout_marginTop="16dp"
46-
android:layout_marginEnd="16dp"
47-
android:checked="true"
48-
android:text="@string/hint_switch_history"
49-
android:visibility="gone"
47+
android:layout_marginBottom="16dp"
48+
app:layout_constraintBottom_toBottomOf="parent"
5049
app:layout_constraintEnd_toEndOf="parent"
51-
app:layout_constraintTop_toBottomOf="@+id/textView" />
50+
app:layout_constraintStart_toStartOf="parent"
51+
app:layout_constraintTop_toBottomOf="@+id/button">
5252

53-
<TextView
54-
android:id="@+id/textView"
55-
android:layout_width="0dp"
56-
android:layout_height="wrap_content"
57-
android:layout_marginLeft="16dp"
58-
android:layout_marginTop="16dp"
59-
android:layout_marginRight="16dp"
60-
android:text="Hello World!"
61-
app:layout_constraintHorizontal_bias="0.0"
62-
app:layout_constraintLeft_toLeftOf="parent"
63-
app:layout_constraintRight_toRightOf="parent"
64-
app:layout_constraintTop_toBottomOf="@+id/button" />
53+
<LinearLayout
54+
android:layout_width="match_parent"
55+
android:layout_height="wrap_content"
56+
android:orientation="vertical">
57+
58+
<androidx.appcompat.widget.SwitchCompat
59+
android:id="@+id/switchHistory"
60+
android:layout_width="wrap_content"
61+
android:layout_height="wrap_content"
62+
android:layout_marginTop="16dp"
63+
android:layout_marginEnd="16dp"
64+
android:checked="true"
65+
android:text="@string/hint_switch_history"
66+
android:visibility="gone" />
67+
68+
<TextView
69+
android:id="@+id/textView"
70+
android:layout_width="match_parent"
71+
android:layout_height="wrap_content"
72+
android:layout_marginLeft="16dp"
73+
android:layout_marginTop="16dp"
74+
android:layout_marginRight="16dp"
75+
android:hint="@string/result"
76+
android:textColorHint="#78909C"
77+
android:textSize="20sp" />
78+
</LinearLayout>
79+
</ScrollView>
6580

6681
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values-en/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<string name="super_button">A Button</string>
55
<string name="hint_input">Chemical</string>
66
<string name="hint_switch_history">History(unavailable)</string>
7+
<string name="result">Result</string>
78
</resources>

app/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<string name="hint_input">在此输入化学式</string>
55
<string name="hint_switch_history">记录历史(未启用)</string>
66
<string name="super_button">按钮</string>
7+
<string name="result">结果...</string>
78
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<string name="super_button">按钮</string>
44
<string name="hint_input">在此输入化学式</string>
55
<string name="hint_switch_history">记录历史(未启用)</string>
6+
<string name="result">结果...</string>
67

78
</resources>

0 commit comments

Comments
 (0)