设置 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>
...以及警告:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的 xml 中尝试此操作:
在您的代码中,将方法签名更改为:
这是 Android按钮教程。
Try this in your xml:
And in your code, change the method signature to:
Here is the Android Button tutorial.
要将某些行为绑定到 UI 按钮,您需要注册一个接收特定事件类型通知的侦听器。在您的情况下,您注册一个 OnClickListener (用于单击事件);就像下面的代码片段一样:
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:
您需要向按钮添加一个点击侦听器。将其放入您的
onCreate()
中:You need to add a click listener to your button. Put this in your
onCreate()
:大多数答案倾向于使用“setOnClickListener”而不是使用 xml 属性。
我个人更喜欢使用 xml 来使项目在 android 中可点击。
您所犯的错误是将您的函数设置为私有。单击该项目后调用的函数应该是公共的。
您应该记住 3 件事:
在 xml 中定义 2 个属性。
android:clickable="true"
android:onClick="functionName"
在 Activity 文件中定义该函数。确保保持该函数公开。
public void 函数名(View v) {
// TODO 自动生成的方法存根
}
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:
Define the 2 properties in xml.
android:clickable="true"
android:onClick="functionName"
Define that function in the Activity file. Make sure to keep the function public.
public void functionName(View v) {
// TODO Auto-generated method stub
}
Make sure to pass 'View v' as an argument for that function.