Android(学生CW)需要指导
public class Menu extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
//myIntent.setClassName("hello.World", "hello.World.mybuttonclick");
// myIntent.putExtra("com.android.samples.SpecialValue", "Hello, Joe!"); // key/value pair, where key needs current package prefix.
//startActivity(myIntent);
//Button myButton = (Button) findViewById(R.id.my_button);
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void updateLayout(){
Intent myIntent = new Intent(Menu.this, mybuttonclick.class);
startActivity(myIntent);
// TextView sayHello = (TextView) findViewById(R.id.Hello);
}
}
嘿伙计们,我是一名新的 android java 学生,我们必须开发一个简单的 hello world 应用程序..我发现在 xml 中使用 android:Onclick 使我的 onClick() 活动正常工作有些困难..我正在尝试做什么更改内容视图确实显示一个不同的布局并打招呼..我正在使用 setContentLayout 来执行此操作,每次我单击所述按钮时 android 应用程序都会崩溃..我做错了什么吗?
问候,
斯特凡
public class Menu extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
//myIntent.setClassName("hello.World", "hello.World.mybuttonclick");
// myIntent.putExtra("com.android.samples.SpecialValue", "Hello, Joe!"); // key/value pair, where key needs current package prefix.
//startActivity(myIntent);
//Button myButton = (Button) findViewById(R.id.my_button);
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void updateLayout(){
Intent myIntent = new Intent(Menu.this, mybuttonclick.class);
startActivity(myIntent);
// TextView sayHello = (TextView) findViewById(R.id.Hello);
}
}
Hey guys, I am a new android java student and we have to develop a simple hello world app.. I am finding some difficulty getting my onClick() activity to work, using android:Onclick in xml.. what i am trying to do is change the content view do display a simply a different layout and saying hello.. i am using setContentLayout to do this, every time i click said button tho the android app crashes out.. am i doing something wrong?
regards,
Stefan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在 xml 中设置点击侦听器时,您必须在您单击的活动中定义该方法。假设您将 xml 中的 onClick 设置为“buttonClicked”,您必须创建一个与下面的方法完全相同的方法。
需要注意的是,该方法是一个 public void,只有一个 View 类型的参数。 XML 定义的点击侦听器必须像这样才能工作。上例中的视图对象是被单击的视图。
When you set a click listener in xml you must have the method defined inside the activity you are clicking in. Lets say you set the onClick in xml to be "buttonClicked", you must create a method looking exactly like the one below.
The thing to notice is that the method is a public void with only a single parameter of type View. XML defined click listeners must be like this to work. The view object in the example above is the view that was clicked.
您更新布局功能需要阅读
You update layout function needs to read
针对您的问题,有很多问题导致了您所描述的复杂情况。首先要说明的是,只要你对某些事情做出让步,你不必以任何特定的方式做任何事情。 Android 是一个非常灵活的平台,而 Java 是一种 OOP 语言,允许您执行许多非 OOP 语言无法执行的操作。
每当您创建一个“可点击”项目(例如
Button
)时,如果您想让程序做出响应,则必须有一些东西“监听”它。这称为侦听器。就您而言,您正在寻找一个OnClickListener
。OnClickListener
不一定必须是Activity
的一部分。它只需是一个实现 View.OnClickListener
的类
即可。然后,您已经告诉Button
的setOnClickListener()
方法它的侦听器是谁。以下示例显示了在没有 XML 声明的情况下所需的内容(但这很重要)。需要注意的是上面的
OnClick()
。每个OnClickListener
必须使用与OnClick()
相同的签名,这意味着即使它具有不同的名称,它也必须具有相同的返回值和相同的参数。对于您想要执行的操作(在 XML 中),您已将android:OnClick
设置为updateLayout
。这意味着 `updateLayout() 必须声明如下:现在,让 update 方法实际工作:当您提供代码时,我们实际上并不知道您遇到了什么错误。如果我们有包含您收到的错误的 Logcat 输出的副本,那么解决问题总是容易得多。一旦我们可以专门针对您的错误,我就可以编辑我的答案以包括您可能额外需要的内容。
模糊逻辑
In response to your question, there are a number of things that are issues causing the complication that you described. Let it first be said, that you don't have to do anything any particular way, provided that you make concessions for certain things. Android is a very flexible platform and Java, as an OOP language allows you to do things that many non OOP languages do not.
Whenever you create a "clickable" item, like a
Button
, if you want to have your program respond, you must have something "listen" to it. This is known as a Listener. In your case, you are looking for anOnClickListener
. TheOnClickListener
does not have to be a part of theActivity
necessarily. It just has to be aclass
thatimplements View.OnClickListener
. Then, you have tell thesetOnClickListener()
method of theButton
who its listener is. The following example shows what is necessary without your declaration in XML (but it is important).Something that needs to be noted is the
OnClick()
above. EveryOnClickListener
must use the same signature as theOnClick()
That means itmust have the same return and same arguments even if it has a different name. For what you are trying to do (in XML), you have set yourandroid:OnClick
toupdateLayout
. This means that `updateLayout() must be declared as follows:Now, getting the update method to actually work: While you provide your code, we don't actually know what errors you are getting. It is always much easier to solve a problem if we have a copy of the Logcat output that includes the error you are receiving. Once, we have that we can target your error specifically and I can edit my answer to include what you may additionally need.
FuzzicalLogic