2013-03-21 58 views
-3

我有单一屏幕上的两个edittexts与具有最大长度11 n以外作为12.两个EDITTEXT验证

现在我想以下这两个edittexts验证之一。

1.只有1个edittext的值应该传递到下一个屏幕。这是必须的。只有一个值必须传递到下一个屏幕。 2.如果用户开始输入任何编辑文本,那么他应该被迫输入最少11或12位数字到各自的编辑文本中。

此验证是在按钮单击屏幕上完成的。

任何想法?

回答

0

您可以在各个领域的EditText XML文件使用

android:maxLength="11" 

android:maxLength="12" 

并将该值传递给下一个活动。你可以使用意图。

在第一项活动:

String s = myEdtxt.getText().toString(); 
Intent i = new Intent(this, NextAct.class); 
i.putStringArrayListExtra("My Value", s); 
startActivity(i); 

您可以在第二个活动检索为:

String s= getIntent().getStringExtra("My Value"); 

希望其明确的。

1

您应该使用EditText onChangeListener重新计算每次用户输入任何文本时的字符数。每次为任一EditText小部件触发onChangeListener时,请计算字符数,并根据字符数启用或禁用移至下一个屏幕的Button。

0

可以验证使用setError属性中编辑框 mUserNameView是我的EditText 和 mUserName是一个字符串,其中这是我在EDITTEXT值来执行验证

mUserNameView.setError(null); 

    // Store values at the time of the login attempt. 
    mUserName = mUserNameView.getText().toString(); 



    // Check for a valid email address. 
    if (TextUtils.isEmpty(mUserName)) { 
     mUserNameView.setError(getString(R.string.error_field_required)); 

    } 
if (mUserName.length() > 13) 
{ 

mUserNameView.setError(getString(R.string.error_field_notValid)); 
} 


R.string.error_field_required 
R.string.error_field_notValid 

在定义值在资源文件夹中的字符串

根据您的需要调整登录名。我已经给出了代码示例如何在代码中进行验证。和(不管你在字符串文件中指定的)错误信息在特定的编辑框中键入一个非常美丽的方式产生,

1

检查以下内容: XML:

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="43dp" 
    android:maxLength="11" 
    android:ems="10" > 

</EditText> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editText1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="44dp" 
    android:maxLength="12" 
    android:ems="10" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editText2" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="130dp" 
    android:text="Button" /> 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/editText1" 
android:layout_below="@+id/editText1" 
android:text="" 
android:visibility="invisible" 
android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
android:id="@+id/textView2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/editText2" 
android:layout_below="@+id/editText2" 
android:text="" 
android:visibility="invisible" 
android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
android:id="@+id/editText3" 
android:layout_width="0dp" 
android:layout_height="0dp" 
android:layout_alignLeft="@+id/editText1" 
android:layout_alignParentTop="true" 
android:ems="10" /> 

和类:

public class MainActivity extends Activity { 
EditText et1,et2,et3; 
Button btn; 
TextView tv1,tv2; 



@Override 
protected void onStart() { 
    et3.requestFocus(); 
    super.onStart(); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    et1=(EditText)findViewById(R.id.editText1); 
    et2=(EditText)findViewById(R.id.editText2); 
    et3=(EditText)findViewById(R.id.editText3); 
    btn=(Button)findViewById(R.id.button); 
    tv1=(TextView)findViewById(R.id.textView1); 
    tv2=(TextView)findViewById(R.id.textView2); 
    addListenerOnTextChangeEt1Text(); 
    addListenerOnTextChangeEt2Text(); 
    et1.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View arg0, boolean hasFocus) {    
      if(!hasFocus){ 
       tv1.setVisibility(View.INVISIBLE); 
       tv1.setVisibility(View.GONE); 
      } 
      else{ 
       tv1.setVisibility(View.VISIBLE); 
      } 
     } 
    }); 
    et2.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View arg0, boolean hasFocus) {    
      if(!hasFocus){ 
       tv2.setVisibility(View.INVISIBLE); 
       tv2.setVisibility(View.GONE); 
      } 
      else{ 
       tv2.setVisibility(View.VISIBLE); 
      } 
     } 
    }); 
    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
        if(et1.getText().toString().length()<11 || et2.getText().toString().length()<12){ 
         Toast.makeText(getBaseContext(), "check both edittext data", Toast.LENGTH_SHORT).show(); 
        } 
        else if(et1.getText().toString().length()==11 && et2.getText().toString().length()==12){ 
         Toast.makeText(getBaseContext(), "Show next Page", Toast.LENGTH_SHORT).show(); 
        } 
        else{ 
         Log.v("Error",""); 
        } 
     } 
    }); 
} 
private void addListenerOnTextChangeEt1Text(){ 
    et1.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      int et1StringLength=et1.getText().toString().length();   
      Log.v("et1StringLength","--->"+et1StringLength); 
      int num=11-(et1StringLength); 
      if(et1StringLength<11){ 
       tv1.setText("Enter"+num+"Characters more");     
      } 
      else if(et1StringLength==11){ 
       tv1.setText("Correct");     
      } 
      else{ 
       Log.v("Error",""); 
      } 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 


} 
private void addListenerOnTextChangeEt2Text(){ 
    et2.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      int et2StringLength=et2.getText().toString().length();   
      Log.v("et2StringLength","--->"+et2StringLength); 
      int num=12-(et2StringLength); 
      if(et2StringLength<12){ 
       tv2.setText("Enter"+num+"Characters more");     
      } 
      else if(et2StringLength==12){ 
       tv2.setText("Correct");     
      } 
      else{ 
       Log.v("Error",""); 
      } 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 


} 

并按照此链接将值传递给下一个意图:here