چک کردن پر یا خالی بودن EditText در اندروید
پنجشنبه, ۶ ارديبهشت ۱۳۹۷، ۱۰:۱۴ ق.ظ
در این مقاله قصد داریم که یک نمونه برای شما قرار دهیم به این صورت که شما می توانید با پر یا خالی بودن edittext خودتان را با استفاده از یک دکمه متوجه شوید که آیا خالی هست یا پر شده است.
برای این سورس ابتدا در کلاس جاوا قطعه کد زیر را قرار دهید:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public class MainActivity extends Activity {
EditText edittext;
Button button;
String GetEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
edittext = (EditText)findViewById(R.id.editText1);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GetEditText = edittext.getText().toString();
if (TextUtils.isEmpty(GetEditText)){
Toast.makeText(MainActivity. this , "EditText is Empty" , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity. this , "EditText is Not Empty" , Toast.LENGTH_LONG).show();
}
}
});
}
} |
کد های XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:paddingBottom= "@dimen/activity_vertical_margin"
android:paddingLeft= "@dimen/activity_horizontal_margin"
android:paddingRight= "@dimen/activity_horizontal_margin"
android:paddingTop= "@dimen/activity_vertical_margin"
tools:context= "com.checkedittextemptyornot_android_examples.com.MainActivity" >
<EditText
android:id= "@+id/editText1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignParentTop= "true"
android:layout_centerHorizontal= "true"
android:layout_marginTop= "139dp"
android:ems= "10" >
</EditText>
<Button
android:id= "@+id/button1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_below= "@+id/editText1"
android:layout_centerHorizontal= "true"
android:layout_marginTop= "28dp"
android:text= "Click Here to Check whether EditText is empty or not in android" />
</RelativeLayout> |
خروجی کار به صورت زیر خواهد بود:
- ۹۷/۰۲/۰۶