2011-04-28 83 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(); 
      } 

     }); 
    }}