2011-07-31 40 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' 作为该函数的参数。