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
accepting click events in RelativeLayout
提问by skooter
I have a RelativeLayout
with a few TextView
as 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 RelativeLayout
as 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
- Give your
RelativeLayout
an ID, like you typedshift_parent_name
Set your
RelativeLayout
XML toandroid: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">
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) ); } });
- 给你
RelativeLayout
一个 ID,就像你输入的一样shift_parent_name
将您的
RelativeLayout
XML设置为android:clickable="true"
您的最终 xml 将如下所示:
<RelativeLayout android:id="@+id/shift_parent_name" android:layout_width="fill_parent" android:layout_weight="0.25" android:clickable="true">
然后在您的 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.this
and About.class
.
确保更改您的活动名称,Main.this
并且About.class
.
The main activity called Main.java
and 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"