Android 在LinearLayout中查找LinearLayout的子级

Android 在LinearLayout中查找LinearLayout的子级,android,android-layout,android-linearlayout,childviews,Android,Android Layout,Android Linearlayout,Childviews,我有一个已经用xml创建的LinearLayout(“ll”),应用程序会在其中动态创建另一个LinearLayout,并在该视图中创建一个EditText和一个按钮。该按钮使整个LinearLayout连同编辑文本和其中的按钮一起销毁自身(整个系统是一个玩家姓名输入活动)。不管怎样,我正在试图找到一种方法,从所有编辑文本中获取文本。我曾尝试在“ll”上使用for循环,并在“ll”上使用ll.getChildAt()但我似乎无法在ll.getChildAt()生成的任何对象上使用.getChil

我有一个已经用xml创建的LinearLayout(“ll”),应用程序会在其中动态创建另一个LinearLayout,并在该视图中创建一个EditText和一个按钮。该按钮使整个LinearLayout连同编辑文本和其中的按钮一起销毁自身(整个系统是一个玩家姓名输入活动)。不管怎样,我正在试图找到一种方法,从所有编辑文本中获取文本。我曾尝试在“ll”上使用for循环,并在“ll”上使用
ll.getChildAt()
但我似乎无法在
ll.getChildAt()
生成的任何对象上使用
.getChildAt()
,因为
getChildAt()
生成的是“视图”而不是“线性布局”。我基本上只需要一种方法来搜索两个孩子,而不是一个。另外,如果有更好的方法,请告诉我。我愿意接受建议

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

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);


    }
}
}
公共类NewGameCreate扩展活动{
int numOfPlayers=0;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(视图){
Numof++;
最终线性布局ll=findViewById(R.id.playerView);
final LinearLayout llNew=新的LinearLayout(getApplicationContext());
llNew.setOrientation(线性布局。水平);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName=新的EditText(此);
newName.setLayoutParams(新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父项,ViewGroup.LayoutParams.WRAP_内容,1));
newName.setHint(“输入玩家名称”);
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
最终按钮删除=新按钮(此按钮);
delete.setLayoutParams(新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容,0));
删除.setText(“删除”);
删除.setId(numOfPlayers);
删除.setWidth(0);
delete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int id=delete.getId();
ll.移除视图InLayout(findViewById(id));
可缩回=ll.getBackground();
ll.setBackgroundColor(00000000);
11.退步地(后);
ll.invalidate();
}
});
llNew.addView(删除);
}
公共void startName(视图){
LinearLayout ll=findViewById(R.id.playerView);
列表文本=新的ArrayList();
for(int loop=0;loop
我想我找到了答案。您需要更改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());
                }
            }

        }
    }
public void startName(视图){
LinearLayout ll=findViewById(R.id.playerView);
列表文本=新的ArrayList();
for(int loop=0;loop
在上面的代码中,您只能获得第一个子级,但由于您在具有垂直方向的父线性布局中添加了具有水平方向的线性布局,因此您已经为父布局的子级(即playerView)编写了代码。我已经修改了代码,以获得子线性布局的元素,并且日志打印EditText中的所有文本


希望有帮助

我想我找到了答案。您需要更改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());
                }
            }

        }
    }
public void startName(视图){
LinearLayout ll=findViewById(R.id.playerView);
列表文本=新的ArrayList();
for(int loop=0;loop
在上面的代码中,您只能获得第一个子级,但由于您在具有垂直方向的父线性布局中添加了具有水平方向的线性布局,因此您已经为父布局的子级(即playerView)编写了代码。我已经修改了代码,以获得子线性布局的元素,并且日志打印EditText中的所有文本


希望有帮助

所以基本上我只需要在
.getChildAt()前面添加
(LinearLayout)
。谢谢你的帮助!这是有效的:)非常乐意帮忙!所以基本上我只需要在
.getChildAt()前面添加
(LinearLayout)
。谢谢你的帮助!这是有效的:)非常乐意帮忙!