2011-07-31 4 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のための私のアプリを作った、そして今私はアンドロイドのためのバージョンを作るしようとしています。 iPhoneのバージョンは、インターフェースビルダーが優れているため、soooがはるかに簡単でした(私はちょうどアクションを作り、それをボタンに接続することができます)ので、これはほとんど不可能です。言い換えれば、ボタンにアクションをどのように接続するのか分かりません。誰かが私が間違っていることを教えてもらえますか?

答えて

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ボタンにバインドするには、特定のイベントタイプの通知を受け取るリスナーを登録する必要があります。あなたのケースでは、Clickイベントの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()でこれを入れて:SO上

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

ほとんどの答えではなく、XMLプロパティを使用しての「setOnClickListener」を使用する傾向があります。 私は個人的にAndroidでクリック可能なアイテムを作成するためにxmlを使用する方が好きです。

あなたの間違いは、あなたの機能をプライベートとして設定していることです。アイテムをクリックした後に呼び出される関数はpublicでなければなりません。

  1. は、XMLで2つのプロパティを定義します。

    は、あなたが心に留めておく必要がある3つの事があります。

    アンドロイド:クリッカブル= "true" を アンドロイド:のonClick = "functionNameを"

  2. は、活動ファイル内でその関数を定義します。関数をpublicにしておいてください。

    ます。public void functionnameと(ビューV){ // TODO自動生成されたメソッドスタブ
    }

  3. は、その関数の引数として 'ビューV' を渡すようにしてください。

関連する問題