如何在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執行應用程式,請開啟專案的其中一個活動檔案,然後單擊工具欄中的執行
圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

點選這裡下載專案程式碼
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP