如何查詢編輯文字值是否以子音開頭?
本例演示瞭如何查詢編輯文字值是否以子音開頭。
步驟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:hint="Enter something" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonPanel" android:text="Click" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
在上面的程式碼中,我們使用了編輯文字和按鈕檢視來驗證編輯文字資料是否以子音開頭。
步驟3 - 將以下程式碼新增到java/MainActivity.xml
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edit_query = findViewById(R.id.edit_query);
findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = edit_query.getText().toString();
if (!data.isEmpty()) {
data = data.substring(0, 1);
if (data.equals("a") || data.equals("e") || data.equals("i") || data.equals("o") || data.equals("u"))
Toast.makeText(MainActivity.this, data + " is vowel", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, data + " is consonant", Toast.LENGTH_LONG).show();
}
}
});
}
}讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要在Android Studio中執行應用程式,請開啟專案中的一個活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

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