开发者

accepting click events in RelativeLayout

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?


Just add a OnClickListener to your RelativeLayout


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.


Add a "OnClickListener" to your RelativeLayout.

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


  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


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

             }
         });


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜