ANDROID OS - EditText komponentasi bilan ishlash

EditText komponentasi android operatsion tizimida ishlaydigan dasturlarning deyarli 75-80 % ida ishlatiladi. Bu komponenta yordamida malumotlarni kiritishimiz mumkin. EditText ga qo'limiz bilan tekkanimizda avtomat ravishda keyboard(mobil klaviatura) paydo bo'ladi, va kursor avtomat ravishda EditText dan joy oladi.

Sardor Dushamov Android

Biz bu komponentaning har xil turlaridan foydalanishimiz mumkin, ya'ni foydalanuvchi faqat sonlarni kiritishi mumkin, yoki password(parol) kiritishi yoki manzil, email va hkz larni kiritishi mumkin.

Agar faqat raqam termoqchi bulsa faqat raqamli klaviatura ko'rinadi, faqat raqamli klaviatura chiqishi uchun biz EditTextning android:inputType atributini o'zgartirishimiz zarur.
Ya'ni quyidagilardan birini qo'ysak buladi:

android:inputType=«text» — oddiy holatdagi klaviatura
android:inputType=«textEmailAddress» — elektron manzil yozish uchun mo'ljallangan klaviatura
android:inputType=«textPassword» — Parol yozish uchun, yani parol terganimizda yozgan matn ko'rinmasdan(*) bo'lib turadi
android:inputType=«number» — faqat raqamli klaviatura

Bu maqolada hammasini yozib o'tirmadim, asosiylarini yozdim, qolganlarini shu yerdan o'qib olsangiz bo'ladi.
Sardor Dushamov Android
Sardor Dushamov Android

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.
Sardor Dushamov Android
«Kirish» tugmasini bosganimizda biz natijani ko'ramiz:
Sardor Dushamov Android

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) {
    }
}

Sardor Dushamov Android
Sardor Dushamov Android
Sardor Dushamov Android

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.

Нет комментариев