서버에서 분류해서 처리하는 어플을 제작중인데요.
한 EditText 에서 자릿수가 4자라면 4자 이하로 입력하면 4자로 입력하라고 toast 박스가 출력되게 하려고 합니다.
그 제한을 어떤식으로 해야 할지 모르겠습니다.
4자 이상은 xml파일에서 간단하게 처리 했는데 4자 이하 입력시가 고민이네요..
아래 소스 참조하세요
제가 직접 작성한 것입니다
package ty.test;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class test extends Activity implements OnClickListener, View.OnTouchListener{
/** Called when the activity is first created. */
EditText test0;
EditText test1;
EditText test2;
int input = 4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test0 = (EditText)findViewById(R.id.test0);
test0.setMovementMethod(null);
test0.setOnTouchListener(this);
test1 = (EditText)findViewById(R.id.test1);
test1.setMovementMethod(null);
test1.setOnTouchListener(this);
test2 = (EditText)findViewById(R.id.test2);
test2.setMovementMethod(null);
test2.setOnTouchListener(this);
View but = (Button)findViewById(R.id.but);
but.setOnClickListener(this);
}
public boolean onTouch(View v, MotionEvent event){
switch(v.getId()){
case R.id.test0:
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
String str_0 = test0.getText().toString();
int num_0 = str_0.length();
if(num_0==input){
test0.setFocusableInTouchMode(false);
test1.requestFocus();
}else{
if(num_0 == 0)
Toast.makeText(getApplicationContext(), "입력값 없음", Toast.LENGTH_SHORT).show();
if(num_0>input)
Toast.makeText(getApplicationContext(), "입력초과", Toast.LENGTH_SHORT).show();
if(num_0<input)
Toast.makeText(getApplicationContext(), "4자리 입력", Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.test1:
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
String str_1 = test1.getText().toString();
int num_1 = str_1.length();
if(num_1==input){
test1.setFocusableInTouchMode(false);
test2.requestFocus();
}else{
if(num_1 == 0)
Toast.makeText(getApplicationContext(), "입력값 없음", Toast.LENGTH_SHORT).show();
if(num_1>input)
Toast.makeText(getApplicationContext(), "입력초과", Toast.LENGTH_SHORT).show();
if(num_1<input)
Toast.makeText(getApplicationContext(), "4자리 입력", Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.test2:
break;
}
return true;
}
public void onClick(View v){
switch(v.getId()){
case R.id.but:
String a = test0.getText().toString();
if(a.equals("bon")){
Toast.makeText(getApplicationContext(), "들어와.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "장난해.", Toast.LENGTH_SHORT).show();
}
break;
}
}
}



