簡體   English   中英

在LinearLayout中查找LinearLayout的子級

[英]Find child of a LinearLayout inside a LinearLayout

我有一個已經在xml中創建的LinearLayout(“ ll”),應用程序在其中動態創建了另一個LinearLayout,並在該視圖內部創建了一個EditText和一個Button。 該按鈕使整個LinearLayout及其內部的EditText和Button自行銷毀(整個系統是一個輸入活動的玩家名稱)。 無論如何,我試圖找到一種方法來從所有EditText中獲取文本。 我曾嘗試在“ ll”上使用for循環並使用ll.getChildAt()但由於ll.getChildAt()生成的內容似乎無法使用.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);


    }
}
}

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

        }
    }

在上面的代碼中,您只能獲得第一個孩子,但是由於您在方向為Vertical的父級LinearLayout中添加了一個Horizo​​ntal方向的linearLayout,因此您為父級布局的子級(即playerView)編寫了代碼。 我已經修改了代碼以獲取子線性布局的元素,並且Log從EditText打印所有文本。

希望有幫助!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM