Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีดึงค่าจาก EditText ใน Android

ภาพรวม

ตัวอย่างพื้นฐานสาธิตวิธีอ่านค่าข้อความจาก EditText และนำไปแสดงใน TextView เมื่อกดปุ่ม

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button"
        android:layout_centerHorizontal="true"
        android:hint="Enter text here"
        android:inputType="text"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Get Value"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:textSize="16sp"
        android:textStyle="bold"/>

</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private TextView textView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ดึงค่าจาก EditText
                String inputText = editText.getText().toString().trim();
                
                // ตรวจสอบว่ามีข้อความหรือไม่
                if (inputText.isEmpty()) {
                    textView.setText("Please enter some text");
                    textView.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                } else {
                    textView.setText("You entered: " + inputText);
                    textView.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                }
            }
        });
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                
                
            </intent-filter>
        </activity>

    </application>
</manifest>

การทดสอบ

รันแอป → พิมพ์ข้อความในช่อง EditText → กดปุ่ม "Get Value" → ข้อความจะแสดงใน TextView ด้านล่าง

วิธีดึงค่าจาก EditText ใน Android วิธีดึงค่าจาก EditText ใน Android

เมธอดที่เกี่ยวข้องกับ EditText

Methodคำอธิบาย
getText()คืนค่า Editable (สามารถแก้ไขได้)
getText().toString()คืนค่า String (ใช้บ่อยที่สุด)
getEditableText()คืนค่า Editable สำหรับการแก้ไขต่อ
setText()ตั้งค่าข้อความ
getError()ดึงข้อความ Error ที่ตั้งไว้
setError()แสดงข้อความผิดพลาดใต้ EditText

InputType ที่นิยมใช้

  • text - ข้อความทั่วไป
  • textEmailAddress - อีเมล (มี @ .com)
  • textPassword - รหัสผ่าน (ซ่อนตัวอักษร)
  • number - ตัวเลข
  • phone - เบอร์โทรศัพท์
  • textMultiLine - หลายบรรทัด