设置 Android 按钮

发布于 2024-11-27 03:30:42 字数 1823 浏览 2 评论 0原文

这是我的问题。我完全按照 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>

...以及警告:

VariDiceActivity 类型中的方法 die1Clicked 从未在本地使用。

我必须说我对 Android 开发完全陌生。我为 iPhone 制作了应用程序,现在我正在尝试为 Android 制作一个版本。 iPhone 版本要简单得多,因为有更好的界面生成器(所以我可以只执行一个操作并将其连接到按钮),所以这对我来说几乎难以理解。换句话说,我不明白如何将操作连接到按钮。有人可以告诉我我做错了什么吗?

Here is my problem. I setup the buttons exactly the way they are setup in the Android documentation, but I am getting a warning, and the button will not do anything.

Here is my Java code:

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

...and the 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>

...and the warning:

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

I must say that I am completely new to Android development. I made my app for the iPhone, and I am now trying to make a version for the android. The iPhone version was sooo much easier, because of the better interface builder (so I can just make an action and connect it to the button that way), so this is almost impossibly hard for me to understand. In other words, I do not understand how you connect an action to the button. Could somebody please tell me what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的他你的她 2024-12-04 03:30:42

在您的 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按钮教程

Try this in your xml:

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

And in your code, change the method signature to:

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

Here is the Android Button tutorial.

梦魇绽荼蘼 2024-12-04 03:30:42

要将某些行为绑定到 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);
    ...
}

To bind some behavior to an UI button, you need to register a listener that receives notifications of a certain event type. In your case, you register a OnClickListener (for the click event); just like in the following snippet:

// 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);
    ...
}
抹茶夏天i‖ 2024-12-04 03:30:42

您需要向按钮添加一个点击侦听器。将其放入您的 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    
});

You need to add a click listener to your button. Put this in your 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    
});
放血 2024-12-04 03:30:42

大多数答案倾向于使用“setOnClickListener”而不是使用 xml 属性。
我个人更喜欢使用 xml 来使项目在 android 中可点击。

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

您应该记住 3 件事:

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

    android:clickable="true"
    android:onClick="functionName"

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

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

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

Most answers on SO tend to use 'setOnClickListener' instead of using xml properties.
I personally prefer using xml for making items clickable in android.

The mistake you have made is setting your function as private. The function which gets called after clicking the item should be public.

There are 3 things you should keep in mind:

  1. Define the 2 properties in xml.

    android:clickable="true"
    android:onClick="functionName"

  2. Define that function in the Activity file. Make sure to keep the function public.

    public void functionName(View v) {
    // TODO Auto-generated method stub
    }

  3. Make sure to pass 'View v' as an argument for that function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文