在LinearLayout中查找LinearLayout的子项

时间:2018-06-06 03:34:10

标签: android android-layout android-linearlayout childviews

我有一个已经在xml中创建的LinearLayout(" ll"),app会在其中动态创建另一个LinearLayout,并在该视图中创建一个EditText和一个Button。该按钮使整个LinearLayout与其中的EditText和Button一起销毁(整个系统是一个输入活动的玩家名称)。无论如何,我试图找到一种方法来获取所有EditTexts的文本。我试过在" ll"上使用for循环。并使用ll.getChildAt()但我无法在.getChildAt()生成的任何内容上使用ll.getChildAt(),因为getChildAt()会生成"视图"不是" LinearLayout。"我基本上只需要一种搜索​​两个孩子的方法,而不仅仅是一个。另外,如果有更好的方法我应该这样做,请告诉我。我愿意接受建议。

这是我的代码,如果有帮助的话:

NewGameCreate.java

public class NewGameCreate extends Activity {

int numOfPlayers = 0;

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

public void newPlayer(View view) {
    numOfPlayers++;
    final LinearLayout ll = findViewById(R.id.playerView);
    final LinearLayout llNew = new LinearLayout(getApplicationContext());
    llNew.setOrientation(LinearLayout.HORIZONTAL);
    llNew.setId(numOfPlayers);
    ll.addView(llNew);

    EditText newName = new EditText(this);
    newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
    newName.setHint("Enter Player Name");
    newName.setId(numOfPlayers);
    newName.setWidth(0);
    llNew.addView(newName);

    final Button delete = new Button(this);
    delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
    delete.setText("Delete");
    delete.setId(numOfPlayers);
    delete.setWidth(0);
    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = delete.getId();
            ll.removeViewInLayout(findViewById(id));
            Drawable back = ll.getBackground();
            ll.setBackgroundColor(00000000);
            ll.setBackground(back);
            ll.invalidate();
        }
    });
    llNew.addView(delete);
}

public void startGame(View view){
    LinearLayout ll = findViewById(R.id.playerView);
    List text = new ArrayList();

    for(int loop = 0; loop < ll.getChildCount(); loop++) {
        //this is the code in question and where I want to get the text from
        //all my EditTexts
        LinearLayout inner = ll.getChildAt(loop);


    }
}
}

1 个答案:

答案 0 :(得分:1)

我想我找到了答案。您需要在startGame()方法中更改一些代码,我在下面提供了startGame的代码。

   public void startGame(View view) {
        LinearLayout ll = findViewById(R.id.playerView);
        List text = new ArrayList();

        for (int loop = 0; loop < ll.getChildCount(); loop++) {
            //this is the code in question and where I want to get the text from
            //all my EditTexts
            LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
            for (int j = 0; j < inner.getChildCount(); j++) {
                if (inner.getChildAt(j) instanceof EditText) {
                    EditText textET = (EditText) inner.getChildAt(j);

                    Log.d("TAG",textET.getText().toString());
                }
            }

        }
    }

在上面的代码中,您只能获得第一个子节点,但是当您在父LinearLayout中以方向Vertical添加了一个方向为Horizo​​ntal的linearLayout时,您已经为父布局的子节点编写了代码,即playerView。我修改了代码以获取子元素的元素线性布局和日志打印EditText中的所有文本。

希望有所帮助!!