设置Android按钮

时间:2011-07-31 23:19:41

标签: java android xml imagebutton

这是我的问题。我按照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 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

要将某些行为绑定到UI按钮,您需要注册一个接收特定事件类型通知的侦听器。在您的情况下,您注册一个OnClickListener(用于click事件);就像在下面的代码片段中一样:

// 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);
    ...
}

答案 2 :(得分: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    
});

答案 3 :(得分:0)

SO上的大多数答案倾向于使用'setOnClickListener'而不是使用xml属性。 我个人更喜欢使用xml来制作可在Android中点击的项目。

您所犯的错误是将您的功能设为私有。单击该项后调用的函数应该是公共的。

你应该记住三件事:

  1. 在xml中定义2个属性。

    机器人:可点击= “真” 机器人:的onClick = “functionName”

  2. 在Activity文件中定义该函数。确保将该功能保持公开。

    public void functionName(View v){         // TODO自动生成的方法存根
        }

  3. 确保将“View v”作为该函数的参数传递。

相关问题