Android EditText bilan ishlash.
Yangi proyekt yaratamiz File ⇒ New ⇒ Android Project
main.xml ni ichiga 2 ta EditText va Button konponentalarini o'rnatamiz.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android EditText"
android:textSize="30dp" android:paddingBottom="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="@+id/textView" android:layout_gravity="left|center_vertical" android:textSize="25dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
android:hint="username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="@+id/textView2" android:layout_gravity="left|center_vertical" android:textSize="25dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password" android:layout_gravity="left|center_vertical" android:hint="password"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Kirish"
android:id="@+id/login"/>
</LinearLayout>
Asosiy EditTextActivity activityni ochamiz va quyidagilarni yozamiz:
package uz.sardordushamov.AndroidEditText;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EditTextActivity extends Activity{
private EditText login;
private EditText password;
private Button kirish_button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
kirish_button = (Button)findViewById(R.id.login);
kirish_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(EditTextActivity.this,
"Login: "+login.getText().toString()+"\n"+
"Password: "+password.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
getText().toString()- ushbu metod bilan biz edit textning qiymatini olamiz yani nima yozgan bo'lsak ushani.
Ushbu yozgan kodimizda biz, 2 ta edittext ga so'z kiritdik va ushbu kiritilgan so'zlarni Toast classi orqali natijani ko'rdik.
"Kirish" tugmasini bosganimizda biz natijani ko'ramiz:
Endi TextWatcher interface bilan tanishib chiqaylik
Bu interfacedan foydalanish uchun 3 ta metodini override qilib olish zarur. Bular:
- afterTextChanged
- beforeTextChanged
- onTextChanged
Bu interfacedan foydalanishdan maqsad edittext orqali biror so'z kiritayotganimizda har bir kiritgan simvol yoki harfdan (oldin yoki keyin) qandaydir operatsiyalar bajarish kerak bo'lganda ushbu interface ni implement qilib ishlatishimiz mumkin. Misol uchun lugat dasturlarida foydalanish mumkin yani biror so'zni kiritishni boshlaganimizda bizga tezda usha kiritlgan harflar bo'yicha soz'larni ko'rsatib beradi
package uz.sardordushamov.AndroidEditText;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class EditTextActivity extends Activity implements TextWatcher {
private EditText password;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
password = (EditText)findViewById(R.id.password);
textView = (TextView)findViewById(R.id.textView2);
password.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
textView.setText("Ismingiz: "+password.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
}
Biror harf yozsak pastidagi TextView da ham o'shani ko'rishimiz mumkin
Bu qanday amalga oshirilyapti?
onTextChanged()metodi orqali buni qilish mumkin,editText da biror o'zgarish bo'lsa ushbu metodni ichiga bajarilish kerak bo'lgan amallarni yozamiz. Bu misolda editText ni qiymatini getText().toString() metodi bilan olib TextView ning setText()beryapmiz.
Har bitta o'zgarishda onTextChanged()metodi ishga tushadi
Dastur kodini ko'chirib olish.
No comments:
Post a Comment