2013-06-24 53 views
-2

我创建了一个显示记分板的应用程序,我想在此应用程序中进行验证。我想验证所有的Edittexts,我该如何做。 我想为EditText上如何验证EditText

public class players extends Activity { 
    LinearLayout player_layout; 
    Bundle b; 

    List<EditText> allEds = new ArrayList<EditText>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.players); 

     b = getIntent().getExtras(); 
     String resStr = b.getString("name"); 

     player_layout = (LinearLayout) findViewById(R.id.player_layout); 

     EditText[] ed1 = new EditText[Integer.parseInt(resStr)+1]; 
     Button add_player = new Button(players.this); 
     add_player.setText("Add Players"); 

     for(int i=1;i<=Integer.parseInt(resStr);i++) { 
      ed1[i] = new EditText(players.this); 
      allEds.add(ed1[i]); 
      player_layout.addView(ed1[i]); 
      ed1[i].setId(i); 
      ed1[i].setHint("enter player" +i+ "name"); 
      ed1[i].setHeight(50); 
      ed1[i].setWidth(300); 
     } 

     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     player_layout.addView(add_player, lp); 

     add_player.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       Intent intent = new Intent(players.this,player_name.class); 
       //Intent intent1 = new Intent(players.this,player_name.class); 
       String[] strings = new String[allEds.size()]; 

       for (int i=0; i < allEds.size(); i++){ 
        strings[i] = allEds.get(i).getText().toString(); 
        // intent.putExtra("playerName",b.getString("name")); 
        intent.putExtra("playerName",strings); 
       } 
       // intent1.putExtra("play",b.getString("name")); 
       startActivity(intent); 
       //startActivity(intent1); 
      } 
     }); 
    } 
} 

回答

0

的数组你可以写正则表达式来验证您的输入字段进行验证。 示例代码

String Pattern = "[a-zA-Z]{5,31}"; 
if(textFromEditText.tostring().matches(Pattern)) { 
//Valid 
} 

这是字符串,可以写自己的正则表达式

0
public void validation(){ 

for(int i=1;i<=Integer.parseInt(resStr);i++) 
    { 
     if(ed1[i].getText("").toString("").equals("")){ 
       Toast.makeText(youractivity.this, "Enter value in EditText"+i, Toast.LENGTH_SHORT).show(); 
       } 

    } 
    }