ListView va ArrayAdapter


Download 97.5 Kb.
bet2/2
Sana24.12.2022
Hajmi97.5 Kb.
#1059923
1   2
Bog'liq
ListView va ArrayAdapter

3- mavzu EditText
EditText elementi TextView sinfining kichik sinfidir. Shuningdek, u matn maydonini ifodalaydi, lekin endi matnni kiritish va tahrirlash imkoniyati bilan. Shunday qilib, EditText-da biz TextView-dagi kabi barcha funktsiyalardan foydalanishimiz mumkin.
TextView mavzusida ko'rib chiqilmagan atributlardan android: hint atributini ta'kidlash kerak. Bu sizga EditText elementi bo'sh bo'lsa, maslahat sifatida ko'rsatiladigan matnni o'rnatish imkonini beradi. Bundan tashqari, biz klaviaturani kiritish uchun sozlash imkonini beruvchi android:inputType atributidan foydalanishimiz mumkin. Xususan, uning ma'nolari orasida quyidagilar mavjud:
text:bir qatorli matn kiritish uchun oddiy klaviatura
textMultiLine: ko'p qatorli matn maydoni
textEmailAddress: @ belgisiga ega bo'lgan oddiy klaviatura elektron pochtaga yo'naltirilgan
textUri: Internet manzillarini kiritish uchun / yo'naltirilgan oddiy klaviatura
textPassword: parol klaviaturasi
textCapWords: yozayotganda, kiritilgan so'zning birinchi belgisi katta harfni, qolganlari kichik harfni ifodalaydi
number: raqamli klaviatura
phone: oddiy telefon uslubidagi klaviatura
date: sanani kiritish klaviaturasi
time: vaqtni kiritish uchun klaviatura
datetime: sana va vaqtni kiritish uchun klaviatura
Biz EditText dan foydalanamiz:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Введите имя"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
android:id="@+id/message"
android:layout_marginTop="16dp"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="Введите сообщение"
android:inputType="textMultiLine"
android:gravity="top"
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />




Bu erda birinchi maydon odatiy bir qatorli, ikkinchisi esa ko'p qatorli. Ikkinchi maydondagi matnni yuqoriga tekislash uchun android:gravity="top" atributi qo'shimcha ravishda o'rnatiladi.

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="34sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Введите имя"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

EditText-ga kiritilgan belgilar darhol TextView elementida ko'rsatiladi deb taxmin qilinadi. Buning uchun biz MainActivity kodini ham o'zgartiramiz:
package com.example.viewapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
TextView textView = findViewById(R.id.textView);
textView.setText(s);
}
});
}
}
Bu yerda addTextChangedListener() usuli matn kiritish tinglovchisini, TextWatcher obyektini EditText elementiga qo‘shadi. Uni ishlatish uchun uchta usulni amalga oshirishimiz kerak, lekin aslida matn o'zgarganda chaqiriladigan onTextChanged usulini amalga oshirish biz uchun etarli. Kiritilgan matn CharSequence parametri sifatida ushbu usulga uzatiladi. Usulning o'zida biz ushbu matnni TextView elementiga o'tkazamiz.
Natijada, EditText-ga kiritilayotganda, barcha belgilar TextView-da ham ko'rsatiladi:

Download 97.5 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling