接受RelativeLayout中的点击事件

sko*_*ter 21 events android onclick android-relativelayout

RelativeLayout和一些TextView孩子一样

<RelativeLayout
    android:id="@+id/shift_parent_name"
    android:layout_width="fill_parent"
    android:layout_weight="0.25"
    >
    <TextView
        android:id="@+id/shift_parent_nametitle"
        android:text="@string/shift_parent_nametitle"
        style="@style/header_text"
        />
    <TextView
        android:id="@+id/shift_parent_namefield"
        android:layout_alignParentRight="true"
        android:layout_below="@id/shift_parent_nametitle"
        style="@style/wrap"
        />
Run Code Online (Sandbox Code Playgroud)

我该如何去使用RelativeLayout一个按钮来向反应click事件如果按下区域的任何部分?

Rof*_*ion 21

只需将OnClickListener添加到RelativeLayout即可


小智 19

我有一个名为"RelativeMain1"的RelativeLayout.这就是我启动Activity的方式

RelativeLayout relativeclic1 =(RelativeLayout)findViewById(R.id.RelativeMain1);
         relativeclic1.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v){
                 startActivityForResult(new Intent(A_My_Galaxy.this,C_Student_Book_Planet.class), 0);
             }
         });
Run Code Online (Sandbox Code Playgroud)

将onClickListener添加到布局后,它应该可以工作.


dhu*_*981 7

向RelativeLayout添加"OnClickListener".

注意:不要忘记添加android:clickable="true"到RelativeLayout.


Iha*_*lly 5

  1. RelativeLayout输入您的ID,就像您键入的一样shift_parent_name
  2. 将您的RelativeLayoutXML 设置为android:clickable="true"

    您的最终xml如下所示:

    <RelativeLayout
    android:id="@+id/shift_parent_name"
    android:layout_width="fill_parent"
    android:layout_weight="0.25"
    android:clickable="true">
    
    Run Code Online (Sandbox Code Playgroud)
  3. 然后在您的onCreate方法中添加代码:

    RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.shift_parent_name);
      relative1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            startActivity(new Intent(Main.this, About.class) );
        }
    });
    
    Run Code Online (Sandbox Code Playgroud)

确保更改您的活动名称,Main.this以及About.class

主要活动称为Main.java,第二个是About.java