【问题标题】:Get TextView in the parent of prent of Button在Button的父级的父级中获取TextView
【发布时间】:2013-08-12 14:52:45
【问题描述】:

我有一个 aLIstView 项目的布局,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:descendantFocusability="afterDescendants"
    android:gravity="right"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:textSize="40sp" 
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"  />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/btn_background" >

        <Button
            android:id="@+id/share_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="2dp"
            android:background="@drawable/delete_btn"
            android:drawableRight="@drawable/delete"
            android:paddingBottom="3dp"
            android:paddingTop="2dp"
            android:text="share"
            android:onClick="myClickHandler" />

    </RelativeLayout>

</LinearLayout>

当用户点击删除按钮(在 myClickHandler() 方法中)时,我想获取 TextView 的文本,

使用此代码:

RelativeLayout vwParentRow = (RelativeLayout)v.getParent();

我得到了Button(RelativeLayout) 的父级,但不知道如何在Button 的父级中获取TextView 的文本

我该怎么做?

谢谢

【问题讨论】:

    标签: android textview android-textattributes


    【解决方案1】:

    我为您找到了解决方案:

    在您的 myClickHandler-method 中,您可以使用以下代码来确定您的 textview 的内容是什么:

    public void myClickHandler(View v) {
        ViewParent relativeLayout = v.getParent();
        ViewParent linearLayout = relativeLayout.getParent();
    
        TextView textView = (TextView) ((View) linearLayout).findViewById(R.id.label);
        String text = textView.getText().toString();
        Log.i("textview content", text);
    }
    

    【讨论】:

      【解决方案2】:

      当用户单击删除按钮时,我想获取 TextView 的文本(在 myClickHandler() 方法中),

      要获取 textView 的文本,请使用:

      String text = ((TextView)findViewById(R.id.label)).getText().toString();
      

      【讨论】:

      • 对不起,我忘了说这个布局是为 ListView 项目设计的
      • 当用户点击您的按钮时,您究竟希望您发生什么?
      • 正是我想分享TextView的文本
      • 查看我的其他答案@odr_m9611
      【解决方案3】:

      在按钮的onClick 中尝试以下操作会怎样:

      TextView myTextView = (TextView) getView().findViewById(R.id.label);
      String text = myTextView.getText().toString();
      

      【讨论】:

      • 对不起,我忘了说这个布局是为 ListView 项目设计的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2011-10-20
      • 2021-12-20
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      相关资源
      最近更新 更多