Android Studio如何实现简易进制转换计算器

发布时间:2022-05-20 16:52:35 作者:iii
来源:亿速云 阅读:240

这篇文章主要介绍了Android Studio如何实现简易进制转换计算器的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android Studio如何实现简易进制转换计算器文章都会有所收获,下面我们一起来看看吧。

1、问题描述

设计并实现一个数制转换器,能够对输入的任- -进制类型的数值转换为指定的数制类型的数值。必须实现的数制类型有二进制、八进制、十进制和十六进制四种。

2、基本要求

(1)使用Spinner 控件,列出四种数制类型,即:二进制、八进制、十进制和十六进制;
(2)“数值”输入框,不使用Android系统提供的输入面板,进行数值输入;且只能是整数数值;

布局代码:

<?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"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="进制转换器"
        android:textSize="35sp"
        android:textColor="#9900AA"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="输入数据:"
        android:textSize="30sp"
        android:textColor="#99CCAA"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_1"
            android:layout_width="125dp"
            android:layout_height="35dp"
            android:text="进制类型:"
            android:textSize="25sp"
            android:textColor="#000"
            />
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="240dp"
            android:layout_height="35dp"
            android:textSize="30sp"
            />
    </LinearLayout>
    <EditText
        android:id="@+id/et_shuru"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></EditText>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="输出数据:"
        android:textSize="30sp"
        android:textColor="#99CCAA"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_2"
            android:layout_width="125dp"
            android:layout_height="35dp"
            android:text="进制类型:"
            android:textSize="25sp"
            android:textColor="#000"
            />
        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="240dp"
            android:layout_height="35dp"
            android:textSize="30sp"
            />
    </LinearLayout>
    <EditText
        android:id="@+id/et_shuchu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></EditText>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:id="@+id/btn_0"
        android:layout_width="70dp"
        android:layout_height="35dp"
        android:text="0"
        android:background="@drawable/btn_1"
        />
        <Button
            android:id="@+id/btn_1"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="1"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_2"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="2"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_3"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="3"
            android:background="@drawable/btn_1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_4"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="4"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_5"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="5"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_6"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="6"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_7"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="7"
            android:background="@drawable/btn_1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_8"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="8"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_9"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="9"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_A"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="A"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_B"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="B"
            android:background="@drawable/btn_1"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_C"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="C"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_D"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="D"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_E"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="E"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_F"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="F"
            android:background="@drawable/btn_1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_CE"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="CE"
            android:background="@drawable/btn_1"
            />
        <Button
            android:id="@+id/btn_js"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:text="计算"
            android:background="@drawable/btn_1"
            />
    </LinearLayout>
</LinearLayout>

按钮按压效果(btn_1.xml)代码

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#CC7A00"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#FF9900"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
</selector>

主代码(MainActivity)

package com.example.hzljinzhi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
    Spinner spinner1,spinner2;
    EditText et_shuru,et_shuchu;
    Button btn_CE,btn_js;
    int ids[]={R.id.btn_0,R.id.btn_1,R.id.btn_2,R.id.btn_3,R.id.btn_4,R.id.btn_5, R.id.btn_6,R.id.btn_7,
            R.id.btn_8,R.id.btn_9,R.id.btn_A,R.id.btn_B,R.id.btn_C,R.id.btn_D,R.id.btn_E,R.id.btn_F};
    String  temp=null,num1=null,num2=null,num3=null,num4=null;int k;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_CE=findViewById(R.id.btn_CE);
        btn_js=findViewById( R.id.btn_js);
       btn_CE.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               et_shuru.setText("");
               et_shuchu.setText("");
           }
       });
        for(int i=0;i<ids.length;i++){
            Button btn = findViewById(ids[i]);

            if(btn != null)
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()){
                           case  R.id.btn_0:
                                et_shuru.setText(et_shuru.getText()+"0");
                            break;
                            case  R.id.btn_1:
                                et_shuru.setText(et_shuru.getText()+"1");
                                break;
                            case  R.id.btn_2:
                                et_shuru.setText(et_shuru.getText()+"2");
                                break;
                            case  R.id.btn_3:
                                et_shuru.setText(et_shuru.getText()+"3");
                                break;
                            case  R.id.btn_4:
                                et_shuru.setText(et_shuru.getText()+"4");
                                break;
                            case  R.id.btn_5:
                                et_shuru.setText(et_shuru.getText()+"5");
                                break;
                            case  R.id.btn_6:
                                et_shuru.setText(et_shuru.getText()+"6");
                                break;
                            case  R.id.btn_7:
                                et_shuru.setText(et_shuru.getText()+"7");
                                break;
                            case  R.id.btn_8:
                                et_shuru.setText(et_shuru.getText()+"8");
                                break;
                            case  R.id.btn_9:
                                et_shuru.setText(et_shuru.getText()+"9");
                                break;
                            case  R.id.btn_A:
                                et_shuru.setText(et_shuru.getText()+"A");
                                break;
                            case  R.id.btn_B:
                                et_shuru.setText(et_shuru.getText()+"B");
                                break;
                            case  R.id.btn_C:
                                et_shuru.setText(et_shuru.getText()+"C");
                                break;
                            case  R.id.btn_D:
                                et_shuru.setText(et_shuru.getText()+"D");
                                break;
                            case  R.id.btn_E:
                                et_shuru.setText(et_shuru.getText()+"E");
                                break;
                            case  R.id.btn_F:
                                et_shuru.setText(et_shuru.getText()+"F");
                                break;
                        }
                    }
                });
        }
        et_shuru=findViewById(R.id.et_shuru);
        et_shuchu=findViewById(R.id.et_shuchu);
       spinner1=(Spinner)findViewById(R.id.spinner1);
       spinner2=(Spinner)findViewById(R.id.spinner2);
        //建立数据源
       String[] mltems = getResources().getStringArray(R.array.data);
        ArrayAdapter<String>adapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mltems);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //绑定Adapter到控件
        spinner1.setAdapter(adapter);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                switch (pos){
                    case 0: k=2;setEnabled(2);break;
                    case 1: k=8;setEnabled(8);break;
                    case 2: k=10;setEnabled(10);break;
                    case 3: k=16;setEnabled(16);break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        spinner2.setAdapter(adapter);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int pos, long id) {
                switch (pos){
                    case 0: btn_js.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                temp = et_shuru.getText().toString();
                                num3 = Integer.valueOf(temp, k).toString();//转换为十进制;
                                num1 = Integer.toBinaryString(Integer.parseInt(num3));
                                et_shuchu.setText(num1);
                            }
                        });break;
                    case 1: btn_js.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                temp = et_shuru.getText().toString();
                                num3 = Integer.valueOf(temp, k).toString();//转换为十进制;
                                num2 = Integer.toOctalString(Integer.parseInt(num3));
                                et_shuchu.setText(num2);
                            }
                        });break;
                    case 2: btn_js.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                temp = et_shuru.getText().toString();
                                num3 = Integer.valueOf(temp, k).toString();//转换为十进制;
                                et_shuchu.setText(num3);
                            }
                        });break;
                    case 3: btn_js.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            temp = et_shuru.getText().toString();
                            num3 = Integer.valueOf(temp, k).toString();//转换为十进制;
                            num4 = Integer.toHexString(Integer.parseInt(num3));
                            et_shuchu.setText(num4);
                        }
                    });break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
    public void setEnabled(int count){
        int i =0,size = Math.min(count,ids.length);
        for( i=0;i<size;i++){
            Button btn = findViewById(ids[i]);
            if(btn != null){
                btn.setEnabled(true);
            }
        }
        for( i=size;i<ids.length;i++){
            Button btn = findViewById(ids[i]);
            if(btn != null){
                btn.setEnabled(false);
            }
        }
    }
}

Spinner 控件的数据源(jinzhi.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="data">
        <item>二进制</item>
        <item>八进制</item>
        <item>十进制</item>
        <item>十六进制</item>
    </string-array>
</resources>

关于“Android Studio如何实现简易进制转换计算器”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android Studio如何实现简易进制转换计算器”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Android Studio如何实现简易计算器
  2. Android怎么实现简易计算器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android studio

上一篇:Python不修改数组怎么找出重复的数字

下一篇:Windows Service 2019使用Docker的问题怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》