Android 在RelativeLayout中接受点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2949211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:01:17  来源:igfitidea点击:

accepting click events in RelativeLayout

androideventsonclickandroid-relativelayout

提问by skooter

I have a RelativeLayoutwith a few TextViewas children

我有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"
        />

How do I go about using the RelativeLayoutas a buttonto react to a click eventif any part of the area is pressed?

如果按下该区域的任何部分,我该如何使用RelativeLayout作为按钮点击事件做出反应?

回答by RoflcoptrException

Just add a OnClickListenerto your RelativeLayout

只需将OnClickListener添加到您的 RelativeLayout

回答by Gimsi

I have a RelativeLayout called "RelativeMain1". This is how i make it start Activity

我有一个名为“RelativeMain1”的RelativeLayout。这就是我让它开始活动的方式

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);
             }
         });

After you add the onClickListener to your layout, it should work.

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

回答by dhuma1981

Add a "OnClickListener" to your RelativeLayout.

将“OnClickListener”添加到您的 RelativeLayout。

Note:Don't forget to add android:clickable="true"to your RelativeLayout.

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

回答by Ihab Shoully

  1. Give your RelativeLayoutan ID, like you typed shift_parent_name
  2. Set your RelativeLayoutXML to android:clickable="true"

    Your final xml will be look like this:

    <RelativeLayout
    android:id="@+id/shift_parent_name"
    android:layout_width="fill_parent"
    android:layout_weight="0.25"
    android:clickable="true">
    
  3. then add the code in your onCreate method :

    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) );
        }
    });
    
  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">
    
  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) );
        }
    });
    

Make sure to change your activity names, Main.thisand About.class.

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

The main activity called Main.javaand the second is About.java

调用的主要活动Main.java和第二个是About.java

回答by Ihab Shoully

RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeMain1);
         relativeclic1.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v){

             }
         });

回答by Derek Dawson

You can simply just add this android:onClick="onClick"

你可以简单地添加这个 android:onClick="onClick"