2022-10-15学习内容
1.案例-计算器-逻辑处理代码
1.1activity_calcular.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:background="#EEEEEE" android:orientation="vertical" android:padding="5dp"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/simple_calculator" android:gravity="center" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:gravity="right|bottom" android:lines="3" android:text="0" android:textColor="@color/black" android:textSize="25sp"/> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="5"> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/cancel" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_divide" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/divide" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_multiply" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/multiply" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/clear" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_seven" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/seven" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_eight" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/eight" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_nine" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/nine" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_plus" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/plus" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_four" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/four" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_five" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/five" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_six" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/six" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_minus" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/minus" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_one" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/one" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_two" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/two" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_three" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/three" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <ImageButton android:id="@+id/ib_sqrt" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:scaleType="centerInside" android:src="@drawable/sqrt" /> <Button android:id="@+id/btn_reciprocal" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/reciprocal" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_zero" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/zero" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_dot" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/dot" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> <Button android:id="@+id/btn_equal" android:layout_width="0dp" android:layout_height="@dimen/button_height" android:layout_columnWeight="1" android:gravity="center" android:text="@string/equal" android:textColor="@color/black" android:textSize="@dimen/button_font_size"/> </GridLayout> </LinearLayout> </ScrollView> </LinearLayout>
1.2strings.xml
<resources> <string name="app_name">chapter03</string> <string name="hello">你好,世界</string> <string name="simple_calculator">简易计算器</string> <string name="cancel">CE</string> <string name="divide">/</string> <string name="multiply">*</string> <string name="clear">C</string> <string name="seven">7</string> <string name="eight">8</string> <string name="nine">9</string> <string name="plus">+</string> <string name="four">4</string> <string name="five">5</string> <string name="six">6</string> <string name="minus">-</string> <string name="one">1</string> <string name="two">2</string> <string name="three">3</string> <string name="reciprocal">1/x</string> <string name="zero">0</string> <string name="dot">.</string> <string name="equal">=</string> </resources>
1.3dimens.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="button_font_size">30sp</dimen> <dimen name="button_height">75dp</dimen> </resources>
1.4CalculatorActivity.java
package com.example.chapter03; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener { private TextView tv_result; // 第一个操作数 private String firstNum = ""; // 运算符 private String operator = ""; // 第二个操作数 private String secondNum = ""; // 当前的计算结果 private String result = ""; // 显示的文本内容 private String showText = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calculator); tv_result = findViewById(R.id.tv_result); // 下面给每个按钮控件都注册了监听器 findViewById(R.id.btn_cancel).setOnClickListener(this); findViewById(R.id.btn_divide).setOnClickListener(this); findViewById(R.id.btn_multiply).setOnClickListener(this); findViewById(R.id.btn_clear).setOnClickListener(this); findViewById(R.id.btn_seven).setOnClickListener(this); findViewById(R.id.btn_eight).setOnClickListener(this); findViewById(R.id.btn_nine).setOnClickListener(this); findViewById(R.id.btn_plus).setOnClickListener(this); findViewById(R.id.btn_four).setOnClickListener(this); findViewById(R.id.btn_five).setOnClickListener(this); findViewById(R.id.btn_six).setOnClickListener(this); findViewById(R.id.btn_minus).setOnClickListener(this); // 减法 findViewById(R.id.btn_one).setOnClickListener(this); findViewById(R.id.btn_two).setOnClickListener(this); findViewById(R.id.btn_three).setOnClickListener(this); findViewById(R.id.btn_reciprocal).setOnClickListener(this); findViewById(R.id.btn_zero).setOnClickListener(this); findViewById(R.id.btn_dot).setOnClickListener(this); findViewById(R.id.btn_equal).setOnClickListener(this); findViewById(R.id.ib_sqrt).setOnClickListener(this); } @Override public void onClick(View v) { String inputText; // 如果是开根号按钮 if (v.getId() == R.id.ib_sqrt) { inputText = "sqrt"; } else { inputText = ((TextView)v).getText().toString(); } switch (v.getId()) { case R.id.btn_clear: clear(); break; case R.id.btn_cancel: break; case R.id.btn_plus: case R.id.btn_minus: case R.id.btn_multiply: case R.id.btn_divide: operator = inputText; refreshText(showText + operator); break; case R.id.btn_equal: // 加减乘除四则运算 double calculate_result = calculateFour(); refreshOperate(String.valueOf(calculate_result)); refreshText(showText + "=" + result); break; case R.id.ib_sqrt: double sqrt_result = Math.sqrt(Double.parseDouble(firstNum)); refreshOperate(String.valueOf(sqrt_result)); refreshText(showText + "sqrt=" + result); break; case R.id.btn_reciprocal: double reciprocal_result = 1.0 / Double.parseDouble(firstNum); refreshOperate(String.valueOf(reciprocal_result)); refreshText(showText + "/=" + result); break; default: // 上次的运算结果已经出来了 if (result.length() > 0 && operator.equals("")) { clear(); } // 无运算符,则继续拼接第一个操作数 if (operator.equals("")) { firstNum = firstNum + inputText; } else { // 有运算符,则继续拼接第二个操作数 secondNum = secondNum + inputText; } // 整数不需要前面的0 if (showText.equals("0") && !inputText.equals(".")) { refreshText(inputText); } else { refreshText(showText + inputText); } break; } } // 加减乘除四则运算,返回计算结果 private double calculateFour() { switch (operator) { case "+": return Double.parseDouble(firstNum) + Double.parseDouble(secondNum); case "-": return Double.parseDouble(firstNum) - Double.parseDouble(secondNum); case "*": return Double.parseDouble(firstNum) * Double.parseDouble(secondNum); default: return Double.parseDouble(firstNum) / Double.parseDouble(secondNum); } } // 清空并初始化 private void clear() { refreshOperate(""); refreshText(""); } // 刷新运算结果 private void refreshOperate(String new_result) { result = new_result; firstNum = result; secondNum = ""; operator = ""; } // 刷新文本显示 private void refreshText(String text) { showText = text; tv_result.setText(showText); } }
1.5效果
2.Activity启动与结束
2.1activity_act_start.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:orientation="vertical" android:gravity="center"> <Button android:id="@+id/btn_act_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到下一个页面"/> </LinearLayout>
2.2activity_act_finish.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:orientation="vertical"> <ImageView android:id="@+id/iv_back" android:layout_width="40dp" android:layout_height="40dp" android:padding="5dp" android:src="@drawable/ic_back" /> <Button android:id="@+id/btn_finish" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="完成"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按返回键,或者点击左上角的箭头图标,或者点击上面的完成按钮,均可关闭当前页面"/> </LinearLayout>
2.3ActStartActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class ActStartActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_start); findViewById(R.id.btn_act_next).setOnClickListener(this); } @Override public void onClick(View v) { startActivity(new Intent(this, ActFinishActivity.class)); } }
2.4ActFinishActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class ActFinishActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_finish); findViewById(R.id.iv_back).setOnClickListener(this); findViewById(R.id.btn_finish).setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.iv_back || v.getId() == R.id.btn_finish) { finish(); } } }
2.5AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chapter04"> <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/Theme.MyApplication"> <activity android:name=".ActFinishActivity" android:exported="false" /> <activity android:name=".ActStartActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2.6效果