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

Android 单击父LinearLayout时如何获取子TextView的值?,android,android-layout,onclicklistener,Android,Android Layout,Onclicklistener,我正在添加任意数量的LinearLayouts,其中包含单个TextView 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("userna

我正在添加任意数量的LinearLayouts,其中包含单个TextView

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 l=(LinearLayout)findViewById(R.id.contacts\u容器);
对于(int i=0;i
我希望能够获取刚刚单击的此LinearLayout的子项TextView的值,我如何才能做到这一点?

尝试以下方法:

TextView tv = textWrapper.findViewById(//TextView Id);
tv.getText().toString();
试试这个:

TextView tv = textWrapper.findViewById(//TextView Id);
tv.getText().toString();

您可以尝试这样的方法,在onClick()中调用它


for(inti=0;i您可以尝试这样的方法,在onClick()中调用它


for(int i=0;i如果您只有一个孩子,则得到如下结果:

   TextView tv = (TextView) textWrapper.getChildAt(0);
   String text = tv.getText().toString();

或用于多用途Spirow答案。

如果您只有一个孩子,则按如下方式获得:

   TextView tv = (TextView) textWrapper.getChildAt(0);
   String text = tv.getText().toString();

或者多用Spirow答案。

只需使用yourChildTextViewName.getText().toString()即可获得;只需使用yourChildTextViewName.getText().toString()即可获得;