问题
I have a RelativeLayout
with a few TextView
as children
<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 button to react to a click event if any part of the area is pressed?
回答1:
Just add a OnClickListener to your RelativeLayout
回答2:
I have a RelativeLayout called "RelativeMain1". This is how i make it start 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);
}
});
After you add the onClickListener to your layout, it should work.
回答3:
Add a "OnClickListener" to your RelativeLayout.
Note: Don't forget to add android:clickable="true"
to your RelativeLayout.
回答4:
- 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) ); } });
Make sure to change your activity names, Main.this
and About.class
.
The main activity called Main.java
and the second is About.java
回答5:
RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeMain1);
relativeclic1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
回答6:
You can simply just add this android:onClick="onClick"
来源:https://stackoverflow.com/questions/2949211/accepting-click-events-in-relativelayout