验证不适用于edittext

时间:2013-06-24 16:23:27

标签: android

我已对单一值进行了一些验证,但它正在运行,但在此代码中,验证无效 请给出一些答案。它没有给出任何错误 它只是进入下一个活动而没有验证任何东西。

package com.example.snooder;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class players extends Activity {





LinearLayout player_layout;
Bundle b;
EditText[] ed1;

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


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.players);

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

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

     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) {
            // TODO Auto-generated method stub

            if (ed1== null )
            {


                for(int i=0; i < allEds.size()+1; i++)
                {
                    if(null== ed1 || ed1[i].length() == 0)
                    {
                        Toast.makeText(getApplicationContext(), "please enter player name", Toast.LENGTH_LONG).show();
                        break;
                    }
                }

            }
            else
            {

                Intent intent = 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",strings);
                }
                startActivity(intent);

            }


        }
    });












}

}

1 个答案:

答案 0 :(得分:0)

我想你会对这个GitHub项目感兴趣:

https://github.com/vekexasia/android-form-edittext