accepting click events in RelativeLayout

元气小坏坏 提交于 2019-12-06 18:29:48

问题


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:


  1. Give your RelativeLayout an ID, like you typed shift_parent_name
  2. Set your RelativeLayout XML 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) );
        }
    });
    

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!