如何在Android中检查编辑文本值是否为字谜?


此示例演示了如何在Android中检查编辑文本值是否为字谜。

步骤 1 - 在Android Studio中创建一个新项目,转到文件⇒新建项目,并填写所有必需的详细信息以创建新项目。

步骤 2 - 将以下代码添加到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:gravity="center"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:orientation="vertical">
   <EditText
      android:id="@+id/edit_query"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
   <EditText
      android:id="@+id/edit_query1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
   <Button
      android:id="@+id/buttonPanel"
      android:text="Button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"></Button>
</LinearLayout>

在上面的代码中,我们使用了两个编辑文本从用户那里获取数据,当用户点击按钮时,它将在Toast上显示结果。

步骤 3 - 将以下代码添加到src/MainActivity.java

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
   EditText edit_query1,edit_query;
   String first,second;
   int found = 0, not_found = 0;
   @RequiresApi(api = Build.VERSION_CODES.P)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      edit_query1=findViewById(R.id.edit_query1);
      edit_query=findViewById(R.id.edit_query);
      findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if(!edit_query.getText().toString().isEmpty() && !edit_query1.getText().toString().isEmpty()) {
               first = edit_query.getText().toString().trim();
               second = edit_query1.getText().toString().trim();
            }
            char[] firstchar=first.toCharArray();
            char[] secondchar=second.toCharArray();
            for(int i = 0;i<firstchar.length;i++) {
               found = 0;
               for(int j = 0;j<secondchar.length;j++){
                  if(firstchar[i] == secondchar[j]) {
                     found = 1;
                     break;
                  }
               }
               if(found == 0) {
                  not_found = 1;
                  break;
               }
            }
            if(not_found == 1) {
               Toast.makeText(MainActivity.this,"it is not anagaram",Toast.LENGTH_LONG).show();
            } else {
               Toast.makeText(MainActivity.this,"it is anagaram",Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}

让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开项目的其中一个活动文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -

点击这里下载项目代码

更新于: 2020年6月26日

97 次浏览

开启你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.