📌  相关文章
📜  如何在 Android Studio 中构建二进制到十进制转换器 Android 应用程序?

📅  最后修改于: 2022-05-13 01:54:38.676000             🧑  作者: Mango

如何在 Android Studio 中构建二进制到十进制转换器 Android 应用程序?

此应用程序会将二进制数转换为十进制数。有些人可能还编写了一个Java程序来将二进制数转换为十进制数,但是在制作应用程序时,让我们看看如何做到这一点。

简要介绍

我们将首先创建一个带有Empty Activity的新项目。创建项目后,我们将添加可绘制资源文件,该文件将用于为 EditText 提供框型形状或轮廓。然后我们将使用XML 文件,我们将在其中添加两个编辑文本、按钮和文本视图。最后,我们将转到MainActivity。 Java并创建一些函数,当用户单击按钮时这些函数将起作用。请注意,我们将使用Java语言实现该应用程序。下面给出了一个示例 gif,以了解我们将在本文中实现的内容。

分步实施

步骤 1:使用空活动创建新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。



第 2 步:添加可绘制资源文件

导航到app/res/drawable并右键单击 drawable 文件夹并转到New/Drawable Resource File

将文件命名为“ edit_text_border ”(您可以根据自己的意愿命名任何内容,但请注意字母不应大写)并使用默认设置,单击“确定”按钮。

第 3 步:使用可绘制资源文件

在可绘制资源“edit_text_border.xml”的XML文件中,我们首先将形状定义为矩形。然后在形状上添加一个描边,这将在形状周围形成一个轮廓。在相同的属性下,我们将指定笔画的宽度和颜色。最后,我们将通过指定角的半径在角中给出一个圆形。请参阅下面的 XML 代码以了解以上几行:



XML


  
    
  
    
  


XML


  
    
  
    


Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Giving name to the variables for two EditTexts and two Buttons
        // input is where the user will input the decimal number
        // output is where the user will get the output in the form of binary number
        // submit is the button created to submit the decimal number entered by the user
        // clear is the button to clear the answer
        EditText input, output;
        Button submit, reset;
  
        // Calling the EditText by id which we gave in xml file
        input = (EditText) findViewById(R.id.editText);
        output = (EditText) findViewById(R.id.output);
  
        submit = (Button) findViewById(R.id.submit);
  
        // It is set so that when the user clicks on submit button, the data
        // gets send in the function created below which will convert it and then
        // show the answer to the user in the output
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // Creating a string method argument
                String string = input.getText().toString();
  
                // Here, we are parsing a string method 
                // argument into an integer object
                int i = Integer.parseInt(string, 2);
  
                // Converts and stores it in the form of string
                String decimal = Integer.toString(i);
  
                // It will show the output in the second edit text that we created
                output.setText(decimal);
            }
        });
  
        // Here, we will define a function which will
        // clear the whole text and reset it
        reset = (Button) findViewById(R.id.reset);
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input.setText("");
                output.setText("");
            }
        });
  
    }
}


步骤 4:使用 activity_main.xml 文件

导航到app/res/layout/activity_main.xml并将以下代码添加到该文件中。在activity_main.xml文件中,我们必须添加两个EditText用于显示输入和输出文本,两个用于提交的按钮将提交输入并清除以重置它,我添加了两个TextView来显示一些文本以便我们可以指导用户关于如何使用这个应用程序。下面是activity_main.xml文件的代码。

XML



  
    
  
    

为应用程序编写 XML 文件的代码后,设计将如下所示:

第 5 步:使用 ActivityMain。 Java文件

转到主活动。 Java文件,参考如下代码。代码中添加了注释以更详细地理解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Giving name to the variables for two EditTexts and two Buttons
        // input is where the user will input the decimal number
        // output is where the user will get the output in the form of binary number
        // submit is the button created to submit the decimal number entered by the user
        // clear is the button to clear the answer
        EditText input, output;
        Button submit, reset;
  
        // Calling the EditText by id which we gave in xml file
        input = (EditText) findViewById(R.id.editText);
        output = (EditText) findViewById(R.id.output);
  
        submit = (Button) findViewById(R.id.submit);
  
        // It is set so that when the user clicks on submit button, the data
        // gets send in the function created below which will convert it and then
        // show the answer to the user in the output
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // Creating a string method argument
                String string = input.getText().toString();
  
                // Here, we are parsing a string method 
                // argument into an integer object
                int i = Integer.parseInt(string, 2);
  
                // Converts and stores it in the form of string
                String decimal = Integer.toString(i);
  
                // It will show the output in the second edit text that we created
                output.setText(decimal);
            }
        });
  
        // Here, we will define a function which will
        // clear the whole text and reset it
        reset = (Button) findViewById(R.id.reset);
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input.setText("");
                output.setText("");
            }
        });
  
    }
}

输出:

图片

视频