欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器設(shè)計(jì)

 更新時(shí)間:2022年05月17日 16:44:25   作者:我叫夏滿滿  
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

一、題目

1、如圖所示(實(shí)際設(shè)計(jì),類似此界面樣式即可,全屏?xí)r,按鈕將會(huì)縱向拉伸),利用網(wǎng)格布局管理器設(shè)計(jì)一個(gè)居中、滿屏計(jì)算器,項(xiàng)目名稱:clc666b;(666,改成自己的實(shí)際編號(hào))

2、加、乘分別用2個(gè)單選按鈕進(jìn)行選擇;

3、為clc666b編寫(xiě)程序(clc666a不需要編程,只設(shè)計(jì)界面即可),根據(jù)選擇的加(乘)單選按鈕,實(shí)現(xiàn)兩個(gè)數(shù)的加法和乘法的簡(jiǎn)單計(jì)算。

4、為了簡(jiǎn)化程序設(shè)計(jì),上方的數(shù)據(jù)區(qū)也可以設(shè)計(jì)成3個(gè)文本框(如果一個(gè)文本框?qū)崿F(xiàn)功能,則更好),分別用作被(乘)加數(shù)、加(乘)數(shù)、合(積);

二、分析

1.首要的目標(biāo)是先做一個(gè)窗口,窗口設(shè)計(jì)需要滿屏平分,所以要修改每一個(gè)部件的權(quán)重。

2.java程序設(shè)計(jì),要監(jiān)聽(tīng)不同種類的按鍵,網(wǎng)上基本上都是普通按鍵的程序,沒(méi)有radiobutton的,這個(gè)題目對(duì)于我這種新手來(lái)說(shuō)有點(diǎn)不太友好,不能直接抄網(wǎng)上的,還要根據(jù)老師上課講的改一改。

(1)當(dāng)按下數(shù)字按鍵時(shí),把按鍵所對(duì)應(yīng)的數(shù)字存到一個(gè)字符串中,然后更新text。

(2)如果按下刪除的時(shí)候把字符串最后一個(gè)字符刪去即可,然后更新text.。

(3)當(dāng)按下運(yùn)算符號(hào)鍵時(shí),把前面的字符存在一個(gè)字符串a(chǎn)中,并保存運(yùn)算符號(hào)鍵的id地址。

(4)繼續(xù)進(jìn)行前兩步的操作,直到按下等于號(hào)鍵運(yùn)行(5)。

(5)把運(yùn)算符號(hào)后的給字符串b,根據(jù)id來(lái)對(duì)a和b進(jìn)行運(yùn)算,更新text。

三、代碼

1.更新后的xml代碼

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
? ? android:layout_row="6"
? ? android:layout_column="3"
? ? android:background="#FF0097A7"
? ? tools:context=".MainActivity">
?
? ? <TextView
? ? ? ? android:id="@+id/textview1"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="2"
? ? ? ? android:layout_columnSpan="3"
? ? ? ? android:layout_columnWeight="3"
? ? ? ? android:background="#F1D5A2"
? ? ? ? android:text=" "
? ? ? ? android:textSize="30sp" />
?
? ? <Button
? ? ? ? android:id="@+id/nm1"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_row="3"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_column="0"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="1"
? ? ? ? android:textSize="36sp" />
?
? ? <Button
? ? ? ? android:id="@+id/nm2"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="2"
? ? ? ? android:textSize="36sp" />
?
? ? <RadioGroup
? ? ? ? android:id="@+id/radioGroup2"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_rowSpan="2"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_gravity="center">
?
? ? ? ? <RadioButton
? ? ? ? ? ? android:id="@+id/mul"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_rowWeight="1"
? ? ? ? ? ? android:layout_columnWeight="1"
? ? ? ? ? ? android:checked="false"
? ? ? ? ? ? android:text="×"
? ? ? ? ? ? android:textSize="36sp" />
?
? ? ? ? <RadioButton
? ? ? ? ? ? android:id="@+id/add"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_rowWeight="1"
? ? ? ? ? ? android:layout_columnWeight="1"
? ? ? ? ? ? android:checked="false"
? ? ? ? ? ? android:text="+"
? ? ? ? ? ? android:textSize="36sp" />
?
? ? </RadioGroup>
?
? ? <Button
? ? ? ? android:id="@+id/nm3"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_row="4"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_column="0"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="3"
? ? ? ? android:textSize="36sp" />
?
? ? <Button
? ? ? ? android:id="@+id/del"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="DEL"
? ? ? ? android:textSize="36sp" />
?
? ? <Button
? ? ? ? android:id="@+id/equ"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_row="5"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_column="0"
? ? ? ? android:layout_columnSpan="2"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="="
? ? ? ? android:textSize="36sp" />
?
</GridLayout>

2.更新后的java代碼

package com.example.lenovo.clc231b;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
?
public class MainActivity extends AppCompatActivity {
?
? ? private String num = "";
? ? private String num_zong = "";
? ? private int fore,back,lenth,id;
? ? TextView textview1;
? ? Button nm1;
? ? Button nm2;
? ? Button nm3;
? ? Button del;
? ? Button equ;
? ? Button mul;
? ? Button add;
?
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
?
? ? ? ? textview1=(TextView)findViewById(R.id.textview1);
?
? ? ? ? nm1 = (Button)findViewById(R.id.nm1);
? ? ? ? nm2 = (Button)findViewById(R.id.nm2);
? ? ? ? nm3 = (Button)findViewById(R.id.nm3);
? ? ? ? del = (Button)findViewById(R.id.del);
? ? ? ? equ = (Button)findViewById(R.id.equ);
? ? ? ? mul = (Button)findViewById(R.id.mul);
? ? ? ? add = (Button)findViewById(R.id.add);
?
? ? ? ? nm1.setOnClickListener(listener);
? ? ? ? nm2.setOnClickListener(listener);
? ? ? ? nm3.setOnClickListener(listener);
? ? ? ? del.setOnClickListener(listener);
? ? ? ? equ.setOnClickListener(listener);
? ? ? ? mul.setOnClickListener(listener);
? ? ? ? add.setOnClickListener(listener);
?
? ? }
? ? //監(jiān)聽(tīng)按鈕
? ? public OnClickListener listener = new OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View view) {
? ? ? ? ? ? switch (view.getId()){
? ? ? ? ? ? ? ? case R.id.nm1:
? ? ? ? ? ? ? ? ? ? number(1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.nm2:
? ? ? ? ? ? ? ? ? ? number(2);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.nm3:
? ? ? ? ? ? ? ? ? ? number(3);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.del:
? ? ? ? ? ? ? ? ? ? delete();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.equ:
? ? ? ? ? ? ? ? ? ? result();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.mul:
? ? ? ? ? ? ? ? ? ? Get_mul_add(1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.add:
? ? ? ? ? ? ? ? ? ? Get_mul_add(2);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? textview1.setText(num);
? ? ? ? }
? ? };
?
? ? private void Get_mul_add(int flag){
? ? ? ? fore = Integer.valueOf(num);
? ? ? ? if(flag == 1) {
? ? ? ? ? ? id = R.id.mul;
? ? ? ? ? ? num += "×";
? ? ? ? }
? ? ? ? else {
? ? ? ? ? ? id = R.id.add;
? ? ? ? ? ? num += "+";
? ? ? ? }
? ? ? ? textview1.setText(num);
? ? ? ? lenth = num.length();
? ? }
?
? ? private void result() {
? ? ? ? num_zong = num;
? ? ? ? num = num.substring(lenth);
? ? ? ? back = Integer.valueOf(num);
? ? ? ? if(id == R.id.mul)
? ? ? ? ? ? num = String.valueOf((fore*back));
? ? ? ? else
? ? ? ? ? ? num = String.valueOf((fore+back));
? ? ? ? num = num_zong + "=" + num;
? ? }
?
? ? private void delete() {
? ? ? ? num = num.substring(0,num.length()-1);
? ? }
?
? ? private void number(int i) {
? ? ? ? num += i;
? ? }
}

3.原來(lái)的代碼

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
? ? android:layout_row="6"
? ? android:layout_column="3"
? ? android:background="#FF0097A7"
? ? tools:context=".MainActivity">
?
? ? <TextView
? ? ? ? android:id="@+id/textview1"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="3"
? ? ? ? android:layout_columnSpan="3"
? ? ? ? android:layout_columnWeight="3"
? ? ? ? android:background="#F1D5A2"
? ? ? ? android:text=" "
? ? ? ? android:textSize="30sp" />
?
? ? <Button
? ? ? ? android:id="@+id/button1"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_row="3"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_column="0"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="1"
? ? ? ? android:textSize="36sp" />
?
? ? <Button
? ? ? ? android:id="@+id/button2"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="2"
? ? ? ? android:textSize="36sp" />
?
? ? <RadioGroup
? ? ? ? android:id="@+id/radioGroup2"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_rowSpan="2"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_gravity="center">
?
? ? ? ? <RadioButton
? ? ? ? ? ? android:id="@+id/r1"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_rowWeight="1"
? ? ? ? ? ? android:layout_columnWeight="1"
? ? ? ? ? ? android:checked="false"
? ? ? ? ? ? android:text="×"
? ? ? ? ? ? android:textSize="36sp" />
?
? ? ? ? <RadioButton
? ? ? ? ? ? android:id="@+id/r2"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_rowWeight="1"
? ? ? ? ? ? android:layout_columnWeight="1"
? ? ? ? ? ? android:checked="false"
? ? ? ? ? ? android:text="+"
? ? ? ? ? ? android:textSize="36sp" />
? ? </RadioGroup>
?
? ? <Button
? ? ? ? android:id="@+id/button3"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_row="4"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_column="0"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="3"
? ? ? ? android:textSize="36sp" />
?
? ? <Button
? ? ? ? android:id="@+id/button4"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="DEL"
? ? ? ? android:textSize="36sp" />
?
? ? <Button
? ? ? ? android:id="@+id/button5"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_row="5"
? ? ? ? android:layout_rowSpan="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_column="0"
? ? ? ? android:layout_columnSpan="2"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:text="="
? ? ? ? android:textSize="36sp" />
?
</GridLayout>
package com.example.lenovo.clc231b;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
?
public class MainActivity extends AppCompatActivity {
?
? ? private String num = "";
? ? private String num_zong = "";
? ? private int fore,back,lenth,id;
? ? TextView textview1;
? ? RadioGroup question2;
? ? Button button1;
? ? Button button2;
? ? Button button3;
? ? Button button4;
? ? Button button5;
?
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
?
? ? ? ? textview1=(TextView)findViewById(R.id.textview1);
?
? ? ? ? button1 = (Button)findViewById(R.id.button1);
? ? ? ? button2 = (Button)findViewById(R.id.button2);
? ? ? ? button3 = (Button)findViewById(R.id.button3);
? ? ? ? button4 = (Button)findViewById(R.id.button4);
? ? ? ? button5 = (Button)findViewById(R.id.button5);
? ? ? ? question2 = (RadioGroup) findViewById(R.id.radioGroup2);
?
? ? ? ? button1.setOnClickListener(listener);
? ? ? ? button2.setOnClickListener(listener);
? ? ? ? button3.setOnClickListener(listener);
? ? ? ? button4.setOnClickListener(listener);
? ? ? ? button5.setOnClickListener(listener);
? ? ? ? question2.setOnClickListener(listener);
?
? ? }
?
? ? public OnClickListener listener = new OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View view) {
? ? ? ? ? ? switch (view.getId()){
? ? ? ? ? ? ? ? case R.id.button1:
? ? ? ? ? ? ? ? ? ? number(1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.button2:
? ? ? ? ? ? ? ? ? ? number(2);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.button3:
? ? ? ? ? ? ? ? ? ? number(3);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.button4:
? ? ? ? ? ? ? ? ? ? delete();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.button5:
? ? ? ? ? ? ? ? ? ? result();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
?
? ? ? ? ? ? question2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onCheckedChanged(RadioGroup group, int checkedId) {
? ? ? ? ? ? ? ? ? ? //獲取被選擇的單選按鈕
? ? ? ? ? ? ? ? ? ? RadioButton r = (RadioButton) findViewById(checkedId);
? ? ? ? ? ? ? ? ? ? //Toast.makeText(MainActivity.this,"你的愛(ài)好是:" + r.getText(), Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? id = r.getId();
? ? ? ? ? ? ? ? ? ? textview1.setText(num + r.getText());
? ? ? ? ? ? ? ? ? ? fore = Integer.valueOf(num);
? ? ? ? ? ? ? ? ? ? num = num+ r.getText();
? ? ? ? ? ? ? ? ? ? lenth = num.length();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? ? textview1.setText(num);
? ? ? ? }
? ? };
//乘號(hào)id是2131165311 ?加號(hào)id是2131165312
? ? private void result()
? ? {
? ? ? ? num_zong = num;
? ? ? ? num = num.substring(lenth);
? ? ? ? back = Integer.valueOf(num);
? ? ? ? if(id == 2131165311)
? ? ? ? ? ? num = String.valueOf((fore*back));
? ? ? ? else
? ? ? ? ? ? num = String.valueOf((fore+back));
? ? ? ? num = num_zong + "=" + num;
? ? }
?
? ? private void delete(){
? ? ? ? num = num.substring(0,num.length()-1);
? ? }
?
? ? private void number(int i){
? ? ? ? num = num + i;
? ? }
}

四、總結(jié)

運(yùn)行必須按照流程來(lái),不然app會(huì)崩潰,不想改了,累了,對(duì)我這樣的新手不太友好。

五、參考文章

 (更新后,更改了一些參數(shù)定義名稱,更加直觀,更改了一些函數(shù)內(nèi)容,減少冗余度)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論