2011-04-28 59 views
0


我在线性布局中有一些TextViews,EditTexts和Buttons。如果我触摸/单击LinearLayout的任何一侧,它将转到下一个活动。我曾尝试为LinearLayout中的所有元素设置onClick侦听器。这有效,但是有什么办法可以编写代码来触摸LinearLayout的任何区域去下一个活动,而无需为所有元素添加onClick处理程序?因为布局中有更多元素。请帮助我。android中的onclick事件问题

回答

0

尝试设置的onClick事件监听器用于从代码linerlayout如下

LinearLayout l1 = (LinearLayout)findViewById(R.id.layout1); 
l1.setOnClickListener(this); 
l1.setClickable(true); 
0

尝试这一个。

login.java

package com.android; 


import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Login extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button newuser = (Button) findViewById(R.id.Button02); 
     newuser.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), Register.class); 
       startActivityForResult(myIntent, 0); 
      } 

     }); 
    } 
} 




Register.java 


package com.android; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Register extends Activity { 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.register); 

     Button register = (Button) findViewById(R.id.Button03); 
     register.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent intent = new Intent(); 
       setResult(RESULT_OK, intent); 
       finish(); 
      } 

     }); 
    }}