การดักค่าใน EditText แบบ AlertDialog หรือ setError
ในวันนี้ผมจะมานำเสนอด้วยกัน 2 แบบครับ นั้นคือ 1. AlerDialog และ 2. setError ซึ่งผมคิดว่าแบบนี้หลายคนก็น่าจะรู้จักอยู่แล้ว. แต่แบบ 2 เนี่ยผมก็เพิ่งรู้จักเหมือนกัน ไปดูกันเลยยยย
AlertDialog |
1. แบบ AlertDialog แบบนี้ก็คือแบบ pop up ขึ้นมานั้นเองโดยเราจะใช้คำสั่ง
final AlertDialog.Builder ad = new AlertDialog.Builder(this); |
แบบนี้ครับ เพื่อเป็นการประกาศ AlertDialog มาเก็บในตัวแปร ad จากนั้นก็ทำการเซ็ตค่าต่อเลย
ad.setTitle("Error! "); ad.setIcon(android.R.drawable.btn_star_big_on); ad.setPositiveButton("Close", null); |
ข้างบนนี่ก็คือการเซ็คค่าของ ad หรือ AlertDialog นั้นเองครับ โดยกำหนด Title เป็น Error! ก็คือแสดงหัวข้อว่า Error!
- setIcon ก็คือการแสดงรูปออกมาจาก Pop up ที่เป็นรูปดาวนั้นเอง.
- setPositiveButton ก็คือสร้างปุ่มที่เป็นข้อความใน AlertDialog ออกมานั้น. โดยใช้คำว่า Close (จะใช้คำไหนก็ได้นะ) ไม่ต้องการให้มันทำอะไรก็ใส่ null ลงไป ปล. แต่ถ้าต้องการให้มันทำคำสั่งอะไรก็ใส่ new DialogInterface.OnClickListener() แล้วทำการ Implement Methods ไปแล้วก็ใส่คำสั่งที่การทำ
แล้วถึงขั้นตอนการดัก ก็ให้ใส่คำสั่งดักไว้ใน Button เพื่อดักข้อมูลที่ไม่พึงประสงค์
if (edt_result1.getText().length() == 0 ) { ad.setMessage("กรุณาเติมคำในช่วงว่างก่อน"); ad.show(); edt_result1.requestFocus(); return; }
คำสั่งก็จะออกมาประมาณนี้. โดยที่ edt_result1 นั้นก็คือ editText ที่เราประกาศ getText().length() == 0 ก็คือไม่มีการใส่ค่าใดๆลงไป. ให้โชว์เป็นเงื่อนไขด้านล่าง. แล้วทำการ return
..........................
setError |
2. แบบ SetError นั้นง่ายกว่าแบบแรกเยอะมาก. ไม่ต้องไปประกาศอะไรให้มันวุ่นวายเลย เพราะการดักข้อมูลนั้น สามารถดักได้ใน editText นั้นเองอยู่แล้ว เพียงใส่คำสั่ง setErrot ลงไปนั้นเอง
if (edt_result2.getText().length() == 0) {edt_result2.setError("กรุณาเติมคำในช่องว่างก่อน");edt_result2.requestFocus();return;}
ตามคำสั่งเลยครับ edt_result2 ก็คือ editText ที่เราได้ประกาศไว้นั้นเอง แล้วใช้ getText().length() == 0 ก็เหมือนเดิมครับ ดักข้อมูลไว้. แล้วใส่ setError เพื่อเป็นข้อความแดงๆ
ดูโค้ดทั้งหมด
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:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.example.tanat29.example.MainActivity"> <EditText android:id="@+id/edt_result1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="20dp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/edt_result2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="20dp" android:textStyle="bold" /> <Button android:id="@+id/btn_check" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="Check" android:textSize="18dp" android:textStyle="bold" /> </LinearLayout>
java
public class MainActivity extends AppCompatActivity { EditText edt_result1,edt_result2; Button btn_check; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt_result1 = findViewById(R.id.edt_result1); edt_result2 = findViewById(R.id.edt_result2); btn_check = findViewById(R.id.btn_check); final AlertDialog.Builder ad = new AlertDialog.Builder(this); ad.setTitle("Error! "); ad.setIcon(android.R.drawable.btn_star_big_on); ad.setPositiveButton("Close", null); btn_check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (edt_result1.getText().length() == 0 ) { ad.setMessage("กรุณาเติมคำในช่วงว่างก่อน"); ad.show(); edt_result1.requestFocus(); return; } if (edt_result2.getText().length() == 0) { edt_result2.setError("กรุณาเติมคำในช่องว่างก่อน"); edt_result2.requestFocus(); return; } Toast.makeText(MainActivity.this, edt_result1.getText().toString()+" "+edt_result2.getText().toString(), Toast.LENGTH_LONG).show(); } }); } }
เป็นยังไงกันบ้างครับ. สำหรับการดักข้อมูลจาก EditText ทั้ง 2 แบบ. เราสามารถใช้ดักแบบไหนก็ได้นะครับ. แล้วแต่จะเลือก. จริงๆแล้วยังมีมากกว่านี้อีกนะ. ที่ผมคุ้นๆมาการใช้ TextWatcher นั้นก็สามารถทำได้. เด่วจะมาอัพเดทให้ทุกๆคนได้อ่านกันอีกนะครับบบบ
🔯Source Code🔯
หน้าแรก โปรเจคแอนดรอยด์
ความคิดเห็น
Find your nearest casinos and hotels near me, easy to reach, and 문경 출장마사지 convenient for you to get to 사천 출장안마 This page 이천 출장안마 uses the most accurate and 오산 출장마사지 current 순천 출장안마 location information from