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

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

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

在本文中,我们将学习如何在Android Studio中使用Java制作十进制到十六进制转换器应用程序。这个应用程序需要输入 a Decimal number ,将其转换为十六进制数,然后显示输出。因此,让我们简要介绍一下我们将如何制作应用程序。

简要通过

我们将首先创建可绘制资源文件并使用它的 XML 为我们将进一步实施的编辑文本提供大纲。之后我们将使用 activity_main.xml ,我们将在其中添加两个 Edit Texts ,一个用于输入十进制数,另一个用于显示输出,两个用于提交的按钮将提交输入并清除以重置它然后是两个文本视图,向用户展示如何使用该应用程序的一些说明。最后,我们将使用 MainActivity。 Java ,其中我们实现了一些功能,当用户点击按钮时,这些功能将完成整个转换。请注意,我们将使用Java编程语言实现整个应用程序。下面是工作应用程序的示例 gif,它将让您了解我们将在本文中制作的内容。

分步实施

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

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。我们将首先选择一个项目模板,然后通过提供名称和选择我们制作应用程序的语言来配置我们的项目。请注意,选择Java作为编程语言。



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

在这一步中,我们将通过导航到app/res/drawable添加一个新的 drawable 资源文件,然后右键单击 drawable 文件夹并转到New/Drawable Resource File 

现在,我们将命名可绘制资源文件选择根元素作为 shape 。使用其余的默认设置,单击“确定”按钮。请注意,在命名资源文件时,名称不应包含任何大写字母

第 3 步:使用 Drawable 资源文件

在上一步创建可绘制资源文件后,现在我们必须在 XML 中编写一些代码,以便为EditText提供大纲。首先,我们将形状定义为“矩形”。然后在形状上添加一个描边,这将在形状周围形成一个轮廓。在相同的属性下,我们将指定笔画的宽度颜色。最后,我们将通过指定角的半径在角中给出一个圆形。请参阅下面的 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 {
  
    // 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;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // 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);
  
                // Converts and stores it in the form of string
                String hexadecimal = Integer.toHexString(i);
  
                // It will show the output in the 
                // second edit text that we created
                output.setText(hexadecimal);
            }
        });
  
        // 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 步:使用 MainActivity。 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 {
  
    // 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;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // 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);
  
                // Converts and stores it in the form of string
                String hexadecimal = Integer.toHexString(i);
  
                // It will show the output in the 
                // second edit text that we created
                output.setText(hexadecimal);
            }
        });
  
        // 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("");
            }
        });
  
    }
}

输出:

图片

视频