2012-09-12 20 views
1

我是新來的android編程。你如何使用android:onClick?我在哪裏放置我想要調用的方法?如何使用android:onClick?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="Button" 
     android:onClick="doSomething"/> 
<ImageView 
    android:id="@+id/icon" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:adjustViewBounds="true" 
    android:src="@drawable/molecule" /> 

我會把它在呼籲要進行此佈局的java文件?

回答

6

是的,你需要把它放在你的setContentView這個佈局的activity類中。 爲的onclick的方法應該是這樣的形式:

public void doSomething(View v) { 
} 
+0

謝謝,這正是我所期待的。 –

2

我假設你制定瞭如何設置您的活動內容?在同一活動中,請添加:

public void doSomething(View view) { 
    // onClick code goes here. 
} 
+0

是的,我已經解決了如何設置內容。感謝您的答覆。這很有幫助。 –

相關問題