【问题标题】:Find child of a LinearLayout inside a LinearLayout在 LinearLayout 中查找 LinearLayout 的子项
【发布时间】:2018-11-15 15:16:51
【问题描述】:

我有一个已在 xml 中创建的 LinearLayout(“ll”),应用程序在其中动态创建另一个 LinearLayout,并在该视图内创建一个 EditText 和一个 Button。该按钮使整个LinearLayout连同其内部的EditText和Button一起销毁(整个系统是一个玩家姓名输入活动)。无论如何,我正在尝试找到一种从所有 EditTexts 中获取文本的方法。我尝试在“ll”上使用 for 循环并使用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);


    }
}
}

【问题讨论】:

    标签: android android-layout android-linearlayout childviews


    【解决方案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 中添加了具有水平方向的线性布局,因此您已经为父布局的孩子编写了代码,即 playerView。我已经修改了代码以获取子线性布局的元素,并且 Log 打印了 EditText 中的所有文本。

    希望有帮助!!

    【讨论】:

    • 所以基本上我只需要在.getChildAt() 前面添加(LinearLayout)。感谢您的帮助!这行得通:)
    • 非常高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    相关资源
    最近更新 更多