6 Şubat 2018 Salı

EditText Sınıfı

Giriş
Sınıfı kullanmak için şu dosya import edilir.
import android.widget.EditText;
Metin düzenlemek için kullanılır. TextView'dan kalıtır.
XML
XML'i şuna benzer.
<EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:text="www.google.com"
        />
XML'de layout_height alanı
Şöyle yaparız.
<EditText
   android:id="@+id/edtFirstName"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="@string/first_name"
   android:padding="5dp"
   android:textColor="@color/black"
   android:textSize="16sp" />
wrap_content'in açıklaması şöyle.
The view should be only big enough to enclose its content .
Mantıken çoklu satırlarda daha çok işe yarar.
android:inputType="textMultiLine"
constructor
Nesne  şöyle alınır.
EditText edt = (EditText)findViewById(R.id.editText1);
addTextChangedListener metodu
TextWatcher Sınıfı yazısına taşıdım.

getId metodu
XM'deki id alanını verir.Şöyle bir xml olsun.
<EditText
   android:id="@+id/etCustomerNo"
id alanının değerini şöyle alırız.
String s = getResources().getResourceEntryName(edt.getId());
getLayoutParams metodu
Şöyle yaparız.
ViewGroup.LayoutParams lp = edt.getLayoutParams();
lp.width = 100;
lp.height = 100;
edt.setLayoutParams(lp);
getText metodu
Metini almak için şöyle yaparız.
Editable text = edt.getText();
getTypeFace metodu
Örnek ver

postDelayed metodu
Bir kodu belli bir süre sonra çalıştırmak için kullanılır.
textView.setVisibility(View.VISIBLE);   
textView.postDelayed(new Runnable() {
               public void run() {
               textView.setVisibility(View.INVISIBLE);
              }
         }, 3000);
setBackgroundResource metodu
Arka plana bir resim şöyle atanır.
// image as Backgound of textview 
edt.setBackgroundResource(R.drawable.img);
setEnabled metodu
Şöyle yaparız.
edt.setEnabled(false);
setDigits metodu
Kabul edileek karakterleri belirtir. XML ile tanımlamak için şöyle yaparız.
<EditText
  ........
  android:inputType="textPersonName"
  android:digits="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY"
/>
setFilters metodu
Elimizde bir Filter olsun."" dönersek input kabul edilmez, null dönersek kabul edilir.
String blockCharacterSet = "~#^|$%&*!"; //Special characters to block

InputFilter filter = new InputFilter() {

  @Override
  public CharSequence filter(CharSequence source, int start, int end,
     Spanned dest, int dstart, int dend) {

    if (source != null && blockCharacterSet.contains(("" + source))) {
      return "";
    }
    return null;
  }
};
Şöyle yaparız.
edt.setFilters(new InputFilter[] { filter });
setGravity metodu
Şöyle yapılır.
// setting gravity to "center"
edt.setGravity(Gravity.CENTER);
Gravity padding ile beraber kullanılabilir.
android:paddingLeft = 16dp
XML'de şöyle tanımlanır.
<EditText
  ...
  android:layout_gravity="center_horizontal"/>  
setHint metodu
XML ile şöyle yaparız.
<EditText
 android:hint = "+6281342134"
 andrioid:text="phone number"
/>
setInputType metodu
Parametre olarak çok fazla seçenek mevcut.
Artı veya eksi sayı alması için şöyle yaparız.
edt.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); //for decimal numbers
edt.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED); //for positive or negative
İkisini birleştirebiliriz. Şöyle yaparız.
edt.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | 
                 InputType.TYPE_NUMBER_FLAG_SIGNED);
MultiLine olması için şöyle yaparız.
<EditText 
...
  android:inputType="textMultiLine"
...
</EditText>
Text için şöyle yaparız.
<EditText
  ...
  android:inputType="text"
/>
setOnClickListener metodu
Şöyle yaparız.
edt.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    ...
  }
});
setOnEditorActionListener metodu
Şöyle yaparız.
edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    ...   
    return false;
    }
});
Örnek - Done/Tamam Düğmesi
Şöyle yaparız
edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  @Override
  public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
    if(i== EditorInfo.IME_ACTION_DONE){
      Toast.makeText(getApplicationContext(),"Done pressed",Toast.LENGTH_SHORT).show();
    }
    return false;
  }
});
Örnek - Done/Tamam Düğmesi
Elimizde şöyle bir XML olsun
<EditText
  android:id="@+id/editText"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:ems="10"
  android:imeOptions="actionDone"
  android:inputType="textPassword" />
Şöyle yaparız.
edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    boolean handled = false;
    if (actionId == EditorInfo.IME_ACTION_DONE) {
      if (v.getText().toString().equals("12345")) {
        ...
      } else {
        ...
        handled = true;
      }
      return handled;
    }
});
setOnFocusChangeListener metodu
Cursor edittext üzerine gelirse veya giderse çağrılır.
Örnek
Şöyle yaparız.
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
      ...
    }
    else {
      ...
    }
  }
});
setOnKeyListener metodu
Şöyle yaparız.
editText.setOnKeyListener(new OnKeyListener() {                 
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    //You can identify which key is pressed
    if(keyCode == KeyEvent.KEYCODE_DEL) {  
        //this is for backspace
    }
    return false;       
  }
});
setOnTouchListener metodu
Şöyle yaparız.
edt.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    ...
    return false;
  };
});
setPaintFlags metodu
Tüm metnin altını çizmek için şöyle yaparız.
edt.setPaintFlags(edt.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
setRawInputType metodu
Şöyle yaparız.
edt.setRawInputType(Configuration.KEYBOARD_12KEY);
Şöyle yaparız.
edt.setRawInputType(Configuration.KEYBOARD_QWERTY);
setSelection metodu
İmleci belirtilen konuma getirir.
Örnek
Şöyle yaparız.
edt.setSelection(position)
Şöyle yaparız.
edt.setSelection(2);
Örnek
Şöyle de yapılabilir.
Selection.setSelection(edt.getText(), edt.getText().length());
Örnek
Şöyle yaparız.
edt.setText("Updated Text From another Activity");
int position = edt.length();
Editable text = edt.getText();
Selection.setSelection(text, position);
setScrollBar metodu
Şöyle yapılır
<EditText
...
  android:scrollbars="vertical"
...
</EditText>
setText metodu
İmzası şöyle
setText(Spannable spannable)
Metin atamak için şöyle yapılabilir.
edt.setText("your Text here"); //set default value

edt.setHint("your Text here"); //set hint
Html gösterilebilir. Html.fromHtml bir Spanned nesnesi döner.
txtview.setText(Html.fromHtml("<u>...</u>");
setTextColor metodu
Şöyle yaparız.
edt.settextcolor("your color code");
setTypeFace metodu
Örnek TextView için verilmiş ancak EditText içinde çalışır. Kullanılan Font'u atar.
TextView textView = ...; 
textView.setTypeface(Typeface.createFromAsset(...));
TypeFace şöyle yaratılır.
Typeface.createFromAsset(context.getAssets(), "fonts/Aclonica.ttf")

Hiç yorum yok:

Yorum Gönder