Android(学生CW)需要指导

发布于 2024-12-09 23:06:53 字数 994 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

友欢 2024-12-16 23:06:53

当您在 xml 中设置点击侦听器时,您必须在您单击的活动中定义该方法。假设您将 xml 中的 onClick 设置为“buttonClicked”,您必须创建一个与下面的方法完全相同的方法。

    public void buttonClicked(View view)
    {
          //Your code here
    }

需要注意的是,该方法是一个 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.

    public void buttonClicked(View view)
    {
          //Your code here
    }

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.

暗喜 2024-12-16 23:06:53

您更新布局功能需要阅读

public void updateLayout(View view)

You update layout function needs to read

public void updateLayout(View view)
溇涏 2024-12-16 23:06:53

针对您的问题,有很多问题导致了您所描述的复杂情况。首先要说明的是,只要你对某些事情做出让步,你不必以任何特定的方式做任何事情。 Android 是一个非常灵活的平台,而 Java 是一种 OOP 语言,允许您执行许多非 OOP 语言无法执行的操作。

每当您创建一个“可点击”项目(例如 Button)时,如果您想让程序做出响应,则必须有一些东西“监听”它。这称为侦听器。就您而言,您正在寻找一个 OnClickListenerOnClickListener 不一定必须是Activity 的一部分。它只需是一个实现 View.OnClickListener即可。然后,您已经告诉 ButtonsetOnClickListener() 方法它的侦听器是谁。以下示例显示了在没有 XML 声明的情况下所需的内容(但这很重要)。

class Menu extends Activity implements View.OnClickListener
{
     public void onCreate(Bundle icicle)
     {    setContentView(R.layout.main);
          Button btn = (Button)findViewById(R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML);
          btn.setOnClickListener(this);
     }

     public void onClick(View view)
     {    int id = view.getId();
          if (id == R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML)
              updateLayout()//Do your Click event here
     }

     public void updateLayout()
     {   //updateLayout code...
     }
}

需要注意的是上面的OnClick()。每个 OnClickListener 必须使用与 OnClick() 相同的签名,这意味着即使它具有不同的名称,它也必须具有相同的返回值和相同的参数。对于您想要执行的操作(在 XML 中),您已将 android:OnClick 设置为 updateLayout。这意味着 `updateLayout() 必须声明如下:

public void updateLayout(View view)

现在,让 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 an OnClickListener. The OnClickListener does not have to be a part of the Activity necessarily. It just has to be a class that implements View.OnClickListener. Then, you have tell the setOnClickListener() method of the Button who its listener is. The following example shows what is necessary without your declaration in XML (but it is important).

class Menu extends Activity implements View.OnClickListener
{
     public void onCreate(Bundle icicle)
     {    setContentView(R.layout.main);
          Button btn = (Button)findViewById(R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML);
          btn.setOnClickListener(this);
     }

     public void onClick(View view)
     {    int id = view.getId();
          if (id == R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML)
              updateLayout()//Do your Click event here
     }

     public void updateLayout()
     {   //updateLayout code...
     }
}

Something that needs to be noted is the OnClick() above. Every OnClickListener 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 your android:OnClick to updateLayout. This means that `updateLayout() must be declared as follows:

public void updateLayout(View view)

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

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