2011-07-31 32 views
1

這是我的問題。我完全按照安裝在Android文檔中的方式設置按鈕,但我收到警告,並且按鈕不會執行任何操作。爲Android設置按鈕

這裏是我的Java代碼:

package com.variDice; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.*; 

public class VariDiceActivity extends Activity { 

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

     //die1Clicked(); 
    } 

    private void die1Clicked() { 
     ImageButton die1button = (ImageButton)findViewById(R.id.die1button); 
     die1button.setImageResource(R.drawable.icon); 
    } 
} 

...和XML:

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

    <ImageView 
     android:id="@+id/varidice_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:src="@drawable/icon"></ImageView> 
    <ImageButton 
     android:id="@+id/die1button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:background="@null"></ImageButton> 

</LinearLayout> 

...並警告:

The method die1Clicked from the type VariDiceActivity is never used locally.

我必須說我對Android開發完全陌生。我爲iPhone製作了我的應用程序,現在我正在嘗試爲Android製作一個版本。由於更好的界面生成器(所以我可以製作一個動作,並按照這種方式將它連接到按鈕上),iPhone版本變得非常簡單,所以這對我來說幾乎是不可能理解的。換句話說,我不明白你如何將一個動作連接到按鈕。有人能告訴我我做錯了什麼嗎?

回答

4

試試這個在您的XML:

<ImageButton 
    android:id="@+id/die1button" 
    android:onClick="die1Clicked" 
    ...></ImageButton> 

而在你的代碼中,方法簽名更改爲:

public void die1Clicked(android.view.View v) { 
    ImageButton die1button = (ImageButton)findViewById(R.id.die1button); 
    die1button.setImageResource(R.drawable.icon); 
} 

這裏是Android Button tutorial

4

要將某些行爲綁定到UI按鈕,您需要註冊一個接收某個事件類型通知的偵聽器。就你而言,你註冊了一個OnClickListener(用於點擊事件);就像下面的代碼片段一樣:

// create the implementation of OnClickListener 
private OnClickListener mDie1Listener = new OnClickListener() { 
    public void onClick(View v) { 
     // do something when the button is clicked 
    } 
}; 

protected void onCreate(Bundle savedValues) { 
    ... 
    // get the button from layout 
    Button button = (Button)findViewById(R.id.die1button); 
    // register the onClick listener with the implementation above 
    button.setOnClickListener(mDie1Listener); 
    ... 
} 
3

您需要爲您的按鈕添加一個點擊監聽器。把這個在您的onCreate()

ImageButton die1button = (ImageButton)findViewById(R.id.die1button); 
die1button.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
    // What to do when the button is clicked  
}); 
0

上所以大多數的答案傾向於使用「setOnClickListener」,而不是使用XML屬性。 我個人更喜歡使用xml在android中使物品可點擊。

您所犯的錯誤是將您的功能設置爲私人。點擊該項目後調用的函數應該是公開的。

有三樣東西,你應該記住:

  1. 在XML定義2個屬性。

    機器人:可點擊= 「真」 安卓的onClick = 「functionName」

  2. 定義在活動文件的功能。確保公開該功能。

    公共無效使用functionName(視圖v){// TODO自動生成方法存根
    }

  3. 確保通過 '視圖V' 作爲該函數的參數。