에디트텍스트

반응형



안드로이드에서 에디트텍스트에 문자를 입력하면 자동완성으로 리스트를 보여주는 기능이다.


이 방법보다 더 나은게 있을지도 모르지만 데이터가 많을 경우 노가다가 필요한 작업인거 같다.


일단 급한대로 이걸 썼다.


국기 이미지 때문에 쓴건데 국가가 너무 많아서 힘들었다 ㅠㅠ


먼저  custom.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"

    >

    <AutoCompleteTextView

        android:layout_height="wrap_content"

        android:layout_width="fill_parent"

        android:id="@+id/edit_national"

        android:hint="Type some text here"/>

</LinearLayout>



그리고 자바 부분에서


import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class CustomActivity implements OnClickListener{

	

	private AutoCompleteTextView autoText;
  	private ArrayList<String> list;
   	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.custom);//해당 xml
	
	autoText= (AutoCompleteTextView)findViewById(R.id.edit_national);

        list = new ArrayList<String>();
        list.add("가나"); //리스트에 문자열을 하나씩 넣는다.자동완성에 필요한 값들이다.
        list.add("가봉");
        list.add("그리스");
        list.add("기니");
        list.add("나이지리아");
	.
	.
	.
	.

   
 
        adapter = new ArrayAdapter<String>(this, 
                android.R.layout.simple_dropdown_item_1line, list);
         
        autoText.setAdapter(adapter);



이렇게 해주면 에디트 텍스트에서 글자를 입력하면 아래에 해당 단어 리스트가 자동으로 뜨게 된다.

이건 가장 단순한 방법이고 좀 더 효율적이 방법이 있을 것이다.



반응형
반응형



안드로이드 EditText 에디트텍스트 값 남겨두기



다음 액티비티로 넘어갔다가 다시 전 액티비티로 돌아올 때 입력했던 EditText 값을 그대로 놔두고 싶다면

intent 호출 부분에



Intent intent = new Intent(this, SecondActivity.class);

startActivity(intent);

finish();


 finish(); 이걸 지워주면 된다.

액티비티 종료 코드를 없애주면 되는 것이다.

난 예제만 보고 따라하느라 그냥 썼다가 값이 안 남길래 삽질을 했었다.

알고보니 간단한 거였다.. 





반응형
반응형

뭔가 특별한게 있을 줄 알았는데 별 거 없었다..


그냥 텍스트 뷰에서 하는 것 처럼 xml 에서


<EditText

android:text="텍스트"

/>


이렇게 해주면 입력칸에 hint가 아닌 진짜 텍스트가 초기상태부터 들어가 있다.




반응형
반응형



사용자로부터 숫자를 입력 받을때 그 값을 검사하고 싶을때 사용한다.

예를 들면 1부터 5까지만 입력을 받고 싶다면 사용자가 5 보다 큰 수를 입력했을때 

값을 초기화 해버리면서 토스트메세지를 즉시 띄우게 된다.


EditText et=(EditText)findViewById(R.id.num); //해당 에디트텍스트 선언

et.addTextChangedListener(new TextWatcher() {

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

// TODO Auto-generated method stub

if(s.toString().length() > 0){ //문자열s의 길이가 0보다 크면

if(Integer.parseInt(s.toString()) > 5){ //문자열s를 int형으로 파싱해서 5보다 크면

et.setText(null);  //에디트텍스트 값을 null로 초기화 해버리고

Toast.makeText(getApplicationContext(), "1부터 5까지만 입력해주세요", Toast.LENGTH_LONG).show();

//토스트메세지를 띄운다.

}

}

}



EditText et=(EditText)findViewById(R.id.num); //해당 에디트텍스트 선언

et.addTextChangedListener(new TextWatcher() {			

@Override

	public void onTextChanged(CharSequence s, int start, int before, int count) {

	// TODO Auto-generated method stub

	if(s.toString().length() > 0){ //문자열s의 길이가 0보다 크면

	if(Integer.parseInt(s.toString()) > 5){ //문자열s를 int형으로 파싱해서 5보다 크면

	et.setText(null);  //에디트텍스트 값을 null로 초기화 해버리고

	Toast.makeText(getApplicationContext(), "1부터 5까지만 입력해주세요", Toast.LENGTH_LONG).show();

//토스트메세지를 띄운다.

		}

	}

}


반응형
반응형

배경이 어두운 색이라면 


에디트텍스트 입력할때 색깔이 비슷해서 알아보기 어렵다. 그래서 색깔을 바꿔줄 필요가 있는데 그 방법은


출처:http://sungho0459.blog.me/40179645913


 <EditText

    android:id="@+id/etRendererTextBody"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:padding="5dp"

    android:textAppearance="?android:attr/textAppearanceMedium"

    android:textColor="#ffFF6000" 

    android:background="#ffffffff"

    android:textCursorDrawable="@null"/>


 <EditText

                                android:id="@+id/etRendererTextBody"

                                android:layout_width="fill_parent"

                                android:layout_height="wrap_content"

                                android:padding="5dp"

                                android:textAppearance="?android:attr/textAppearanceMedium"

                                android:textColor="#ffFF6000" 

                                android:background="#ffffffff"

                                android:textCursorDrawable="@null"/>


android:textCursorDrawable속성값을 @null로 주고

android:textColor속성값을 부여하게 되면 textColor와 같은 색깔로 커서가 깜빡이게 됨

[출처] android EditText 커서 색깔|작성자 에몬



커서색깔은 물론 쓰여지는 글씨 색깔도 바뀐다.




반응형

+ Recent posts