gpt4 book ai didi

android - 单击父 LinearLayout 时如何获取子 TextView 的值?

转载 作者:行者123 更新时间:2023-11-29 15:07:42 26 4
gpt4 key购买 nike

我正在添加任意数量的包含单个 TextView 的 LinearLayout。

LinearLayout l = (LinearLayout) findViewById(R.id.contacts_container);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String username = object.getString("username");

// add linearLayout text wrapper to main wrapper
LinearLayout textWrapper = new LinearLayout(getApplicationContext());
textWrapper.setOrientation(LinearLayout.VERTICAL);
textWrapper.setBackgroundResource(R.drawable.orangeborder);
textWrapper.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textWrapper.setLayoutParams(params);
textWrapper.setId(userId);
textWrapper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// when this linearlayout is clicked
// get the value of this child textview with an id of R.id.contactUsername
// start new intent and pass the username

//TextView tv_id = (TextView) ((View) v.getParent()).findViewById(R.id.contact);
Intent intent = new Intent(ContactsActivity.this, ProfileActivity.class);
intent.putExtra("username", "this username");
startActivity(intent);
}
});
l.addView(textWrapper);

// add username TextView to textWrapper
TextView usernameText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setId(R.id.contactUsername);
usernameText.setText(fullName);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(usernameText);
}

我想获得刚刚被点击的这个 LinearLayout 的子 TextView 的值,我该怎么做?

最佳答案

你可以尝试这样的事情,在你的 onClick() 中调用它

for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
View nextChild = ((ViewGroup)v).getChildAt(i);
if(nextChild.getId() == R.id.your_id){
String text = ((TextView) nextChild).getText().toString();
}
}

关于android - 单击父 LinearLayout 时如何获取子 TextView 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35360975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com