中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android?Studio如何實現簡易進制轉換計算器

發布時間:2022-05-20 16:52:35 來源:億速云 閱讀:241 作者:iii 欄目:開發技術

這篇文章主要介紹了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如何實現簡易進制轉換計算器”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

奉节县| 南开区| 兴义市| 宿州市| 曲水县| 攀枝花市| 惠水县| 昭通市| 新营市| 东乡族自治县| 张家川| 克山县| 调兵山市| 昌吉市| 焉耆| 吴忠市| 瑞金市| 陈巴尔虎旗| 五大连池市| 景泰县| 大邑县| 淳化县| 新闻| 巴彦淖尔市| 昭觉县| 元江| 沁水县| 上虞市| 睢宁县| 鄂伦春自治旗| 临高县| 张家口市| 大宁县| 鄂尔多斯市| 溧阳市| 长岭县| 麦盖提县| 红河县| 玉林市| 金寨县| 南郑县|