序言
跟着CSDN学院的安卓开发课做了一个计算器。
包括以下功能:
1.实现简单的四则运算,并有淡入淡出效果
2.清零和退格
3.横竖屏不同UI界面
4.右上角关于按钮放置相关信息
5.软件语言随着系统语言的改变而改变
文章目录
- 序言
- AS文件目录
- AboutActivity.java
- MainActivity.java
- fade_in.xml
- fade_out.xml
- digital_btn_selector.xml
- symbol_dark_selector.xml
- symbol_light_selector.xml
- activity_about.xml
- activity_main.xml
- digital_btn_area.xml
- display_area.xml
- symbol_dark_btn_area.xml
- symbol_light_btn_area.xml
- layout-land里的activity_main.xml
- menu.xml
- colors.xml
- dimens.xml
- strings.xml
- styles.xml
- values-zh里的strings.xml
- AndroidManifest.xml
一些ADB软件截图
AS文件目录
复制粘贴以下代码应该可以复刻一个计算器
AboutActivity.java
package com.wangyi.diary;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class AboutActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
PackageManager manager = getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(getPackageName(), 0);
}catch (PackageManager.NameNotFoundException e){
e.printStackTrace();
}
String version = info == null? getString(R.string.unknown): info.versionName;
String msg = String.format(getString(R.string.version), version);
TextView ver = findViewById(R.id.version_info);
ver.setText(msg);
}
}
MainActivity.java
package com.wangyi.diary;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.accessibilityservice.GestureDescription;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.javia.arity.Symbol;
import org.javia.arity.Symbols;
import org.javia.arity.SyntaxException;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView cal = (TextView) findViewById(R.id.cal_area);
cal.setFilters(new InputFilter[] { new InputFilter.LengthFilter(14) });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
public void onClick(View view) {
//Toast.makeText(this, "你点击了",Toast.LENGTH_SHORT).show();
// TextView tv = (TextView) findViewById(R.id.cal_area);
// Paint paint = new Paint();
// paint.setTextSize(tv.getTextSize());
// float size = paint.measureText(tv.getText().toString());
// if (size == 14)
// Toast.makeText(this, "MAXSIZE",Toast.LENGTH_SHORT).show();
switch (view.getId())
{
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_add:
case R.id.btn_sub:
case R.id.btn_mul:
case R.id.btn_div:
case R.id.btn_dot:
{
Button btn = (Button) view;
String strAdded = btn.getText().toString();
TextView cal = findViewById(R.id.cal_area);
String strContent = cal.getText().toString();
String strNewContent = strContent + strAdded;
cal.setText(strNewContent);
}break;
case R.id.btn_c:
{
TextView cal = (TextView) findViewById(R.id.cal_area);
cal.setText("");
TextView result = (TextView) findViewById(R.id.result_area);
result.setText("");
}break;
case R.id.btn_del:
{
TextView cal = findViewById(R.id.cal_area);
String strContent = cal.getText().toString();
if (strContent.length() > 0)
{
strContent = strContent.substring(0, strContent.length() - 1);
cal.setText(strContent);
}
}break;
case R.id.btn_equal:
{
TextView cal = findViewById(R.id.cal_area);
String strContent = cal.getText().toString();
try {
Symbols s = new Symbols();
double res = s.eval(strContent);
TextView result = findViewById(R.id.result_area);
result.setText(String.valueOf(res));
Animation fadeIn = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
result.startAnimation(fadeIn);
Animation fadeOut = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out);
cal.startAnimation(fadeOut);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
TextView cal = findViewById(R.id.cal_area);
cal.setText("");
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
catch (SyntaxException e)
{
String str = MainActivity.this.getString(R.string.error_info);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
}break;
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
TextView cal = findViewById(R.id.cal_area);
String strCal = cal.getText().toString();
outState.putString("KEY_CAL_AREA", strCal);
TextView result = findViewById(R.id.result_area);
String strRes = result.getText().toString();
outState.putString("KEY_RES_AREA", strRes);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
TextView cal = findViewById(R.id.cal_area);
String strCal = savedInstanceState.getString("KEY_CAL_AREA");
cal.setFilters(new InputFilter[] { new InputFilter.LengthFilter(23) });
cal.setText(strCal);
TextView res = findViewById(R.id.result_area);
String strRes = savedInstanceState.getString("KEY_RES_AREA");
res.setText(strRes);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_about:
{
Intent i = new Intent(this,AboutActivity.class);
startActivity(i);
}
break;
}
return true;
}
}
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration = "1500"
android:interpolator = "@android:anim/linear_interpolator"/>
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration = "1500"
android:interpolator = "@android:anim/linear_interpolator"/>
</set>
digital_btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="@color/digital_btn_not_pressed">
</item>
<item
android:state_pressed="true"
android:drawable="@color/digital_btn_pressed">
</item>
</selector>
symbol_dark_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="@color/colorSymbolDark_btn_not_pressed">
</item>
<item
android:state_pressed="true"
android:drawable="@color/colorSymbolDark_btn_pressed">
</item>
</selector>
symbol_light_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="@color/colorSymbolLight_btn_not_pressed">
</item>
<item
android:state_pressed="true"
android:drawable="@color/colorSymbolLight_btn_pressed">
</item>
</selector>
activity_about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAppBG">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/version_info"
style="@style/AboutContentStyle"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
android:background="@color/colorAppBG">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
style="@style/TextAreaStyle">
<include layout="@layout/display_area"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
<include layout="@layout/symbol_light_btn_area"/>
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="0dp">
<include layout="@layout/digital_btn_area"/>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<include layout="@layout/symbol_dark_btn_area"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
digital_btn_area.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_weight="1">
<Button
android:id="@+id/btn_7"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_7"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_8"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_8"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_9"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_9"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
</TableRow>
<TableRow
android:layout_weight="1">
<Button
android:id="@+id/btn_4"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_4"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_5"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_5"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_6"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_6"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
</TableRow>
<TableRow
android:layout_weight="1">
<Button
android:id="@+id/btn_1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_1"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_2"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_3"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
</TableRow>
<TableRow
android:layout_weight="1">
<Button
android:id="@+id/btn_0"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_0"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_dot"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/btn_dot"
style="@style/DigitalBtnStyle"
android:onClick="onClick"></Button>
</TableRow>
</merge>
display_area.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/result_area"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="@style/TextAreaStyle">
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/colorDisplayDivider"/>
<TextView
android:id="@+id/cal_area"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:maxLength="14"
style="@style/TextAreaStyle">
</TextView>
</merge>
symbol_dark_btn_area.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn_mul"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="@string/btn_mul"
style="@style/SymbolDarkBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_sub"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="@string/btn_sub"
style="@style/SymbolDarkBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_add"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="@string/btn_add"
style="@style/SymbolDarkBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_equal"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="@string/btn_equ"
style="@style/SymbolDarkBtnStyle"
android:onClick="onClick">
</Button>
</merge>
symbol_light_btn_area.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_c"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/btn_c"
style="@style/SymbolLightBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_del"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/btn_del"
style="@style/SymbolLightBtnStyle"
android:onClick="onClick"></Button>
<Button
android:id="@+id/btn_div"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/btn_div"
style="@style/SymbolLightBtnStyle"
android:onClick="onClick"></Button>
</merge>
layout-land里的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
android:background="@color/colorAppBG">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical"
style="@style/TextAreaStyle">
<include layout="@layout/display_area"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<TableLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
<include layout="@layout/digital_btn_area"/>
</TableLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<include layout="@layout/symbol_dark_btn_area"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical">
<include layout="@layout/symbol_light_btn_area"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item xmlns:app="http://schemas.android.com/apk/res-auto"
app:showAsAction = "never"
android:id="@+id/menu_about"
android:title="@string/about"/>
</menu>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="digital_btn_not_pressed">#D0DCE3</color>
<color name="digital_btn_pressed">#BED1DB</color>
<color name="colorSymbolDark_btn_not_pressed">#66A1B4</color>
<color name="colorSymbolDark_btn_pressed">#78CCE6</color>
<color name="colorSymbolLight_btn_not_pressed">#BED1D8</color>
<color name="colorSymbolLight_btn_pressed">#66A1B4</color>
<color name="colorAppBG">#FF4B5459</color>
<color name="colorDisplayDivider">#FF5C6265</color>
<color name="colorBtnText">#FF000000</color>
<color name="colorDisplayText">#FFFFFFFF</color>
</resources>
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activicty_horizontal_margin">16dp</dimen>
<dimen name="activicty_vertical_margin">16dp</dimen>
<dimen name="btnTextSize">35sp</dimen>
<dimen name="displayTextSize">45sp</dimen>
<dimen name="btnMargin">0.5dp</dimen>
</resources>
strings.xml
<resources>
<string name="app_name">WANG Calculation</string>
<string name="btn_c">C</string>
<string name="btn_del">DEL</string>
<string name="btn_div">/</string>
<string name="btn_7">7</string>
<string name="btn_8">8</string>
<string name="btn_9">9</string>
<string name="btn_4">4</string>
<string name="btn_5">5</string>
<string name="btn_6">6</string>
<string name="btn_1">1</string>
<string name="btn_2">2</string>
<string name="btn_3">3</string>
<string name="btn_dot">.</string>
<string name="btn_0">0</string>
<string name="btn_mul">*</string>
<string name="btn_sub">-</string>
<string name="btn_add">+</string>
<string name="btn_equ">=</string>
<string name="error_info">Illegal Input!</string>
<string name="about">About</string>
<string name="unknown">UnKnown</string>
<string name="version">Version: %s Designer: SuperWG</string>
</resources>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="BaseBtnStyle">
<item name="android:layout_margin">@dimen/btnMargin</item>
<item name="android:textSize">@dimen/btnTextSize</item>
<item name="android:textColor">@color/colorBtnText</item>
</style>
<style name="DigitalBtnStyle" parent="@style/BaseBtnStyle">
<item name="android:background">@drawable/digital_btn_selector</item>
</style>
<style name="SymbolDarkBtnStyle" parent="@style/BaseBtnStyle">
<item name="android:background">@drawable/symbol_dark_selector</item>
</style>
<style name="SymbolLightBtnStyle" parent="@style/BaseBtnStyle">
<item name="android:background">@drawable/symbol_light_selector</item>
</style>
<style name="TextAreaStyle">
<item name="android:textSize">@dimen/displayTextSize</item>
<item name="android:textColor">@color/colorDisplayText</item>
<item name="android:gravity">center_vertical|right</item>
</style>
<style name="AboutContentStyle">
<item name="android:textColor">@color/colorDisplayText</item>
<item name="android:textSize">@dimen/displayTextSize</item>
<item name="android:padding">5dp</item>
<item name="android:gravity">center</item>
</style>
</resources>
values-zh里的strings.xml
<resources>
<string name="app_name">老王计算器</string>
<string name="btn_c">清零</string>
<string name="btn_del">退格</string>
<string name="btn_div">/</string>
<string name="btn_7">7</string>
<string name="btn_8">8</string>
<string name="btn_9">9</string>
<string name="btn_4">4</string>
<string name="btn_5">5</string>
<string name="btn_6">6</string>
<string name="btn_1">1</string>
<string name="btn_2">2</string>
<string name="btn_3">3</string>
<string name="btn_dot">.</string>
<string name="btn_0">0</string>
<string name="btn_mul">*</string>
<string name="btn_sub">-</string>
<string name="btn_add">+</string>
<string name="btn_equ">=</string>
<string name="error_info">输入有误!</string>
<string name="about">关于</string>
<string name="unknown">未知</string>
<string name="version">版本号: %s 作者: 王一</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wangyi.diary">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AboutActivity">
</activity>
</application>
</manifest>
应用调用了arity-2.1.2的sdk,请自行下载,放入lib文件夹下即可。
以上 如果此篇博客对您有帮助欢迎点赞与转发 有疑问请留言或私信 2020/6/28