본문 바로가기

# 02/Android

[Android] EditText

반응형
<EditText
android:id="@+id/memberName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:layout_marginTop="10sp"
android:layout_marginRight="20sp"
android:background="@drawable/join_et"
android:hint="@string/Name"
android:inputType="text"
android:paddingLeft="15sp"
android:paddingTop="15sp"
android:paddingRight="10sp"
android:paddingBottom="15sp"
android:textColor="@color/Black"
android:textSize="18sp"
android:privateImeOptions="defaultInputmode=Korean"
android:singleLine = "true"
android:lines = "1"/>



<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:layout_marginTop="10sp"
android:layout_marginRight="20sp"
android:background="@drawable/join_et"
android:hint="@string/Phone"
android:inputType="phone"
android:paddingLeft="15sp"
android:paddingTop="15sp"
android:paddingRight="10sp"
android:paddingBottom="15sp"
android:textColor="@color/Black"
android:textSize="18sp"
android:singleLine = "true"
android:lines = "1"/>



<EditText
android:id="@+id/product_price"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_marginRight="10sp"
android:layout_weight="1"
android:background="@drawable/join_et"
android:hint="@string/Price"
android:inputType="number"
android:paddingLeft="15sp"
android:paddingTop="15sp"
android:paddingRight="15sp"
android:paddingBottom="15sp"
android:textColor="@color/Black"
android:textSize="18sp"
android:singleLine = "true"
android:lines = "1"/>



/* 가격 콤마 */
private DecimalFormat decimalFormat = new DecimalFormat("#,###");
private String result="";



/* 가격 콤마 */
binding.productPrice.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>19){
binding.productPrice.setText(binding.productPrice.getText().toString().substring(0, 19));
binding.productPrice.setSelection(binding.productPrice.length());
} else {
if(!TextUtils.isEmpty(s.toString()) && !s.toString().equals(result)) {
result = decimalFormat.format(Double.parseDouble(s.toString().replaceAll(",", "")));
binding.productPrice.setText(result);
binding.productPrice.setSelection(result.length());
}
}

}
@Override
public void afterTextChanged(Editable s) {
}
});




<EditText
android:id="@+id/search_text"
android:layout_width="0sp"
android:layout_height="30sp"
android:paddingLeft="10sp"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:layout_weight="1"
android:background="@drawable/search_et"
android:hint="@string/SearchEtHint"
android:privateImeOptions="defaultInputmode=Korean"
android:singleLine = "true"
android:lines = "1"
android:imeOptions="actionSearch"
/>



/* 키보드 버튼으로 검색 하기 */
binding.searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_SEARCH) {
goPreSearch();
return true;
}
return false;
}
});


/* 상품명 글자 수 체크 */
binding.productName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>15){
binding.productName.setText(binding.productName.getText().toString().substring(0, 15));
binding.productName.setSelection(binding.productName.length());
}
}
@Override
public void afterTextChanged(Editable s) {
int textByte = binding.productName.length()*2;
binding.productNameByte.setText(textByte+"");
}
});




/* EditText 글자수 제한 */
setLimitTextLength(binding.memberId, 15);



/* EditText 글자수 제한 */
private void setLimitTextLength(EditText editText, int maxTextLength) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>maxTextLength){
editText.setText(editText.getText().toString().substring(0, maxTextLength));
editText.setSelection(editText.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}


반응형

'# 02 > Android' 카테고리의 다른 글

[Android] 자바로 텍스트, 이미지 바꾸기  (0) 2019.09.30
[Android] 슬립 모드 방지  (0) 2019.09.26
[Android] 버튼 비활성화/활성화  (0) 2019.09.23
[Android] AlertDialog  (0) 2019.09.23
[Android] 스피너  (0) 2019.09.19