📌  相关文章
📜  如何创建摩尔斯电码转换器 Android 应用程序?

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

如何创建摩尔斯电码转换器 Android 应用程序?

摩尔斯电码是一种可以使用点和破折号对字符和文本进行编码的方式。国际摩尔斯电码包含 26 个字母和一些非英文字母,即阿拉伯数字以及一些标点符号。在摩尔斯电码中,大小写字母没有区别。莫尔斯电码的传输以点持续时间来衡量。摩尔斯电码转换器应用程序是用于将给定语句转换为摩尔斯电码的应用程序。这是通过使用 Android Studio 完成的。使用的语言是JavaXML
莫尔斯电码封面

因此,在本文中,让我们使用Java语言创建一个摩尔斯电码转换器 Android 应用程序。该项目还涉及将摩尔斯电码转换为相关语句。这意味着可以使用此 Android 应用程序进行编码和解码。
莫尔斯电码转换器

所需的软件工具

本项目所需的软件工具有:

  1. ANDROID-STUDIO IDE (1.0.2)
  2. 具有 API 级别 21 的 SDK(最低版本)
  3. Java 7 及以上
  4. 安卓智能手机——4.2.2版(果冻豆及以上)(仅用于测试软件)

方法

第 1 步:创建一个新项目

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

第 2 步:创建圆形按钮

此步骤是可选的(仅当您需要带圆角的黑色按钮时)。在项目选项卡中,在屏幕的左上角,单击应用程序文件夹。然后,单击res文件夹。然后,右键单击可绘制文件夹并选择新建,然后选择可绘制资源文件。给这个资源文件一个名字。请记住,资源文件的名称只能使用小写字母。编写下面的代码为带圆角的黑色按钮创建一个资源文件。将此资源文件用作按钮的 XML 代码中的背景,以获取带圆角的黑色按钮。

mybutton.xml


    
        
            
            
            
        
    


activity_main.xml


  
    
  
    
      
    
    
  
        


MainActivity.java
package com.example.morseconverter;
  
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 {
      
  // initialize variables
  EditText etinput,
  etoutput;
  Button btnEncode,
  btnDecode,
  btnclear;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  
    // Assign variables
    etinput = findViewById(R.id.etinput);
    etoutput = findViewById(R.id.etoutput);
    btnDecode = findViewById(R.id.btndecode);
    btnEncode = findViewById(R.id.btnencode);
    btnclear = findViewById(R.id.btnclear);
  
    // initializing string arrays
    final String[] AlphaNumeric = new String[37];
      
    // string array for storing alphabets and numbers
    final String[] AlphaNumeric1 = new String[37];
      
    // string array for storing corresponding morse code
    // assigning alphabets to the string array Alphanumeric[]
    AlphaNumeric[0] = "A";
    AlphaNumeric[1] = "B";
    AlphaNumeric[2] = "C";
    AlphaNumeric[3] = "D";
    AlphaNumeric[4] = "E";
    AlphaNumeric[5] = "F";
    AlphaNumeric[6] = "G";
    AlphaNumeric[7] = "H";
    AlphaNumeric[8] = "I";
    AlphaNumeric[9] = "J";
    AlphaNumeric[10] = "K";
    AlphaNumeric[11] = "L";
    AlphaNumeric[12] = "M";
    AlphaNumeric[13] = "N";
    AlphaNumeric[14] = "O";
    AlphaNumeric[15] = "P";
    AlphaNumeric[16] = "Q";
    AlphaNumeric[17] = "R";
    AlphaNumeric[18] = "S";
    AlphaNumeric[19] = "T";
    AlphaNumeric[20] = "U";
    AlphaNumeric[21] = "V";
    AlphaNumeric[22] = "W";
    AlphaNumeric[23] = "X";
    AlphaNumeric[24] = "Y";
    AlphaNumeric[25] = "Z";
    AlphaNumeric[26] = "0";
    AlphaNumeric[27] = "1";
    AlphaNumeric[28] = "2";
    AlphaNumeric[29] = "3";
    AlphaNumeric[30] = "4";
    AlphaNumeric[31] = "5";
    AlphaNumeric[32] = "6";
    AlphaNumeric[33] = "7";
    AlphaNumeric[34] = "8";
    AlphaNumeric[35] = "9";
    AlphaNumeric[36] = " ";
  
    // assigning the corresponding morse code
    // for each letter and number to 
    // Alphanumeric1[] array
    AlphaNumeric1[0] = ".-";
    AlphaNumeric1[1] = "-...";
    AlphaNumeric1[2] = "-.-.";
    AlphaNumeric1[3] = "-..";
    AlphaNumeric1[4] = ".";
    AlphaNumeric1[5] = "..-.";
    AlphaNumeric1[6] = "--.";
    AlphaNumeric1[7] = "....";
    AlphaNumeric1[8] = "..";
    AlphaNumeric1[9] = ".---";
    AlphaNumeric1[10] = "-.-";
    AlphaNumeric1[11] = ".-..";
    AlphaNumeric1[12] = "--";
    AlphaNumeric1[13] = "-.";
    AlphaNumeric1[14] = "---";
    AlphaNumeric1[15] = ".--.";
    AlphaNumeric1[16] = "--.-";
    AlphaNumeric1[17] = ".-.";
    AlphaNumeric1[18] = "...";
    AlphaNumeric1[19] = "-";
    AlphaNumeric1[20] = "..-";
    AlphaNumeric1[21] = "...-";
    AlphaNumeric1[22] = ".--";
    AlphaNumeric1[23] = "-..-";
    AlphaNumeric1[24] = "-.--";
    AlphaNumeric1[25] = "--..";
    AlphaNumeric1[26] = "-----";
    AlphaNumeric1[27] = ".----";
    AlphaNumeric1[28] = "..---";
    AlphaNumeric1[29] = "...--";
    AlphaNumeric1[30] = "....-";
    AlphaNumeric1[31] = ".....";
    AlphaNumeric1[32] = "-....";
    AlphaNumeric1[33] = "--...";
    AlphaNumeric1[34] = "---..";
    AlphaNumeric1[35] = "----.";
    AlphaNumeric1[36] = "/";
  
    btnEncode.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View v) {
            
        // When button encode is clicked then the
        // following lines inside this curly 
        // braces will be executed
          
        // to get the input as string which the user wants to encode
        String input = etinput.getText().toString();
  
        String output = "";
          
        // variable used to compute the output
        // to get the length of the input string
        int l = input.length();
          
        // variables used in loops
        int i, j;
          
        for (i = 0; i < l; i++) {
          
          // to extract each Token of the string at a time
          String ch = input.substring(i, i + 1);
            
          // the loop to check the extracted token with 
          // each letter and store the morse code in 
          // the output variable accordingly
          for (j = 0; j < 37; j++) {
                
            if (ch.equalsIgnoreCase(AlphaNumeric[j])) {
                  
              // concat space is used to separate
              // the morse code of each token
              output = output.concat(AlphaNumeric1[j]).concat(" ");
                
                
            }
          }
        }
          
        // to display the output
        etoutput.setText(output);
      }
    });
    btnclear.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View v) {
        // When button clear is clicked then the 
        // following lines inside this curly 
        // braces will be executed
          
        // to clear the etinput
        etinput.setText("");
          
        // to clear etoutput
        etoutput.setText("");
      }
    });
    btnDecode.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View v) {
        // When button decode is clicked then the
        // following lines inside this curly 
        // braces will be executed
          
        // to get the input given by the user as string
        String input1 = etinput.getText().toString();
          
        // to add space to the end of the string 
        // because of the logic used in decoding
        String input = input1.concat(" ");
          
        // to get the length of the input string
        int l = input.length();
          
        // i and j are integer variables used in loops. 
        // Variable p is used as the end index of
        // substring() function
        int i, j, p = 0;
         
        // variable used as a starting
        // index of substring() function
        int pos = 0;
          
        // to store the extracted morse code 
        // for each Alphabet,number or space
        String letter = "";
          
        // a to store the output in it
        String output = "";
          
        for (i = 0; i < l; i++) {
              
          // a variable used to trigger the j loop only
          // when the complete morse code of a letter
          // or number is extracted    
          int flag = 0;
            
          // to extract each token at a time
          String ch = input.substring(i, i + 1);
           
          // if the extracted token is a space
          if (ch.equalsIgnoreCase(" ")) {
              
            // to store the value of i in p
            p = i;
              
            // to extract the morse code for each letter or number
            letter = input.substring(pos, p);
              
            // to update the value of pos so that next
            // time the morse code for the next letter 
            // or digit is extracted
            pos = p + 1;
             
            flag = 1;
          }
          String letter1 = letter.trim();
          // to delete extra whitespaces at 
          // both ends in case there are any
          if (flag == 1) {
            for (j = 0; j <= 36; j++) {
              if (letter1.equalsIgnoreCase(AlphaNumeric1[j])) {
                output = output.concat(AlphaNumeric[j]);
                break;
              }
            }
          }
        }
        // to display the output
        etoutput.setText(output);
      }
    });
  }
}


如果想使用默认按钮或其他按钮,则跳过此可选步骤,并且不要在按钮的背景中包含资源文件。

第 3 步:使用 activity_main.xml 文件

  • 打开activity_main.xml 文件并开始编写xml 代码。
  • 在 xml 文件中创建 2 个 EditTexts。一个用于写入输入,另一个用于显示输出。使用 EditTexts 是因为单击 EditTexts 可以选择和复制文本,这对于使用任何消息传递应用程序向其他人发送编码/未编码消息非常有用。
  • 制作 3 个按钮:编码、解码和清除。
    • 编码按钮将输入作为输入 EditText 中的文本,然后将其编码为莫尔斯电码并显示在输出 EditText 中。
    • 解码按钮将莫尔斯电码作为输入 EditText 的输入,然后将其解码为字母和数字并显示在输出 EditText 中。
    • 清除按钮将清除输入和输出的 EditTexts。
  • 为所有 EditTexts 和 Buttons 提供 id。

完整的xml代码如下:

activity_main.xml



  
    
  
    
      
    
    
  
        

第 4 步:使用 MainActivity。Java

  • 打开主活动。 Java文件。
  • 初始化 MainActivity 类下的变量(语法:- Edit Text etinput, etoutput;Button btnEncode, btnDecode, btnclear;)
  • 在 OnCreate 方法下分配变量。
  • 制作一个数组并将所有字母和数字存储在其中。
  • 制作另一个数组并将每个字母和数字的莫尔斯电码存储在与第一个数组中的字母和数字相对应的索引中。
  • 使用下面代码中的逻辑来完成莫尔斯电码转换器应用程序。

下面是MainActivity 的完整代码。 Java文件。

主要活动。Java

package com.example.morseconverter;
  
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 {
      
  // initialize variables
  EditText etinput,
  etoutput;
  Button btnEncode,
  btnDecode,
  btnclear;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  
    // Assign variables
    etinput = findViewById(R.id.etinput);
    etoutput = findViewById(R.id.etoutput);
    btnDecode = findViewById(R.id.btndecode);
    btnEncode = findViewById(R.id.btnencode);
    btnclear = findViewById(R.id.btnclear);
  
    // initializing string arrays
    final String[] AlphaNumeric = new String[37];
      
    // string array for storing alphabets and numbers
    final String[] AlphaNumeric1 = new String[37];
      
    // string array for storing corresponding morse code
    // assigning alphabets to the string array Alphanumeric[]
    AlphaNumeric[0] = "A";
    AlphaNumeric[1] = "B";
    AlphaNumeric[2] = "C";
    AlphaNumeric[3] = "D";
    AlphaNumeric[4] = "E";
    AlphaNumeric[5] = "F";
    AlphaNumeric[6] = "G";
    AlphaNumeric[7] = "H";
    AlphaNumeric[8] = "I";
    AlphaNumeric[9] = "J";
    AlphaNumeric[10] = "K";
    AlphaNumeric[11] = "L";
    AlphaNumeric[12] = "M";
    AlphaNumeric[13] = "N";
    AlphaNumeric[14] = "O";
    AlphaNumeric[15] = "P";
    AlphaNumeric[16] = "Q";
    AlphaNumeric[17] = "R";
    AlphaNumeric[18] = "S";
    AlphaNumeric[19] = "T";
    AlphaNumeric[20] = "U";
    AlphaNumeric[21] = "V";
    AlphaNumeric[22] = "W";
    AlphaNumeric[23] = "X";
    AlphaNumeric[24] = "Y";
    AlphaNumeric[25] = "Z";
    AlphaNumeric[26] = "0";
    AlphaNumeric[27] = "1";
    AlphaNumeric[28] = "2";
    AlphaNumeric[29] = "3";
    AlphaNumeric[30] = "4";
    AlphaNumeric[31] = "5";
    AlphaNumeric[32] = "6";
    AlphaNumeric[33] = "7";
    AlphaNumeric[34] = "8";
    AlphaNumeric[35] = "9";
    AlphaNumeric[36] = " ";
  
    // assigning the corresponding morse code
    // for each letter and number to 
    // Alphanumeric1[] array
    AlphaNumeric1[0] = ".-";
    AlphaNumeric1[1] = "-...";
    AlphaNumeric1[2] = "-.-.";
    AlphaNumeric1[3] = "-..";
    AlphaNumeric1[4] = ".";
    AlphaNumeric1[5] = "..-.";
    AlphaNumeric1[6] = "--.";
    AlphaNumeric1[7] = "....";
    AlphaNumeric1[8] = "..";
    AlphaNumeric1[9] = ".---";
    AlphaNumeric1[10] = "-.-";
    AlphaNumeric1[11] = ".-..";
    AlphaNumeric1[12] = "--";
    AlphaNumeric1[13] = "-.";
    AlphaNumeric1[14] = "---";
    AlphaNumeric1[15] = ".--.";
    AlphaNumeric1[16] = "--.-";
    AlphaNumeric1[17] = ".-.";
    AlphaNumeric1[18] = "...";
    AlphaNumeric1[19] = "-";
    AlphaNumeric1[20] = "..-";
    AlphaNumeric1[21] = "...-";
    AlphaNumeric1[22] = ".--";
    AlphaNumeric1[23] = "-..-";
    AlphaNumeric1[24] = "-.--";
    AlphaNumeric1[25] = "--..";
    AlphaNumeric1[26] = "-----";
    AlphaNumeric1[27] = ".----";
    AlphaNumeric1[28] = "..---";
    AlphaNumeric1[29] = "...--";
    AlphaNumeric1[30] = "....-";
    AlphaNumeric1[31] = ".....";
    AlphaNumeric1[32] = "-....";
    AlphaNumeric1[33] = "--...";
    AlphaNumeric1[34] = "---..";
    AlphaNumeric1[35] = "----.";
    AlphaNumeric1[36] = "/";
  
    btnEncode.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View v) {
            
        // When button encode is clicked then the
        // following lines inside this curly 
        // braces will be executed
          
        // to get the input as string which the user wants to encode
        String input = etinput.getText().toString();
  
        String output = "";
          
        // variable used to compute the output
        // to get the length of the input string
        int l = input.length();
          
        // variables used in loops
        int i, j;
          
        for (i = 0; i < l; i++) {
          
          // to extract each Token of the string at a time
          String ch = input.substring(i, i + 1);
            
          // the loop to check the extracted token with 
          // each letter and store the morse code in 
          // the output variable accordingly
          for (j = 0; j < 37; j++) {
                
            if (ch.equalsIgnoreCase(AlphaNumeric[j])) {
                  
              // concat space is used to separate
              // the morse code of each token
              output = output.concat(AlphaNumeric1[j]).concat(" ");
                
                
            }
          }
        }
          
        // to display the output
        etoutput.setText(output);
      }
    });
    btnclear.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View v) {
        // When button clear is clicked then the 
        // following lines inside this curly 
        // braces will be executed
          
        // to clear the etinput
        etinput.setText("");
          
        // to clear etoutput
        etoutput.setText("");
      }
    });
    btnDecode.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View v) {
        // When button decode is clicked then the
        // following lines inside this curly 
        // braces will be executed
          
        // to get the input given by the user as string
        String input1 = etinput.getText().toString();
          
        // to add space to the end of the string 
        // because of the logic used in decoding
        String input = input1.concat(" ");
          
        // to get the length of the input string
        int l = input.length();
          
        // i and j are integer variables used in loops. 
        // Variable p is used as the end index of
        // substring() function
        int i, j, p = 0;
         
        // variable used as a starting
        // index of substring() function
        int pos = 0;
          
        // to store the extracted morse code 
        // for each Alphabet,number or space
        String letter = "";
          
        // a to store the output in it
        String output = "";
          
        for (i = 0; i < l; i++) {
              
          // a variable used to trigger the j loop only
          // when the complete morse code of a letter
          // or number is extracted    
          int flag = 0;
            
          // to extract each token at a time
          String ch = input.substring(i, i + 1);
           
          // if the extracted token is a space
          if (ch.equalsIgnoreCase(" ")) {
              
            // to store the value of i in p
            p = i;
              
            // to extract the morse code for each letter or number
            letter = input.substring(pos, p);
              
            // to update the value of pos so that next
            // time the morse code for the next letter 
            // or digit is extracted
            pos = p + 1;
             
            flag = 1;
          }
          String letter1 = letter.trim();
          // to delete extra whitespaces at 
          // both ends in case there are any
          if (flag == 1) {
            for (j = 0; j <= 36; j++) {
              if (letter1.equalsIgnoreCase(AlphaNumeric1[j])) {
                output = output.concat(AlphaNumeric[j]);
                break;
              }
            }
          }
        }
        // to display the output
        etoutput.setText(output);
      }
    });
  }
}

输出:在模拟器上运行

Github 链接:点击这里