2012-07-10 23 views

回答

0

onClickListener()方法允許您處理「點擊事件」。當你點擊一個對象時,你可以在這個方法下面有語句。

您可以訪問here獲得更多有關這些方法的知識。

0

如果您使用在XML中定義的onClick()函數,則必須將onClick()方法放入當前的「活動」中。例如:

main.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent"> 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onclick:doSomething() /> 
</LinearLayout> 

Main.java

public class Main extends Activity { 

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

    public void doSomething(){ 
    //do something here 
    } 
    } 

,如果你使用的是碎片上面只會Main.Java

這是重要的搜索DoSomething的,因爲設置onclick監聽器的編程方式(使用setOnClickListener())可能會處理片段onCreateView()中點擊次數的方法,將不會被發現,如果從XML引用。

0

@pronay比斯瓦斯當你想爲這樣的事情的解釋只是把鼠標光標放在文本和彈出對話框將會出現,將告訴你關於特定在Android的事情,如果你使用eclipse

什麼setOnClickListener確實

void android.view.View.setOnClickListener(OnClickListener l) 



public void setOnClickListener (View.OnClickListener l) 
Since: API Level 1 
Register a callback to be invoked when this view is clicked. If this view is not 
clickable, it becomes clickable. 

Parameters 
    The callback that will run 

android.vie w.View.OnClickListener

// working of View.OnClickListener 


    public static interface 
    View.OnClickListener 
    android.view.View.OnClickListener 
    Known Indirect Subclasses 
    CharacterPickerDialog, KeyboardView, QuickContactBadge 
    CharacterPickerDialog Dialog for choosing accented characters related to a base character. 
    KeyboardView A view that renders a virtual Keyboard. 
    QuickContactBadge Widget used to show an image with the standard QuickContact badge and on-click behavior. 



Class Overview 
Interface definition for a callback to be invoked when a view is clicked. 

// OnClickListener 

@Override 


Specified by: onClick(...) in OnClickListener 
public abstract void onClick (View v) 
Since: API Level 1 
Called when a view has been clicked. 

Parameters 
v The view that was clicked.