【问题标题】:How do I add another activity in the main .java file如何在主 .java 文件中添加另一个活动
【发布时间】:2011-12-05 20:05:29
【问题描述】:

这是我在我的主 java 文件中的代码。我将如何制作另一个按钮来连接活动。如果我表达得不够好,请告诉我。我只需要在主 java 文件中添加另一个按钮。我已经在清单中有一个活动和一个类,我只需要将另一个代码块放入主 java 文件中。按钮的id是p40,xml布局的名字是p40.xml,类叫p40.java,活动叫p40。这是我目前的代码:

package com.duncan.hello.world;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.duncan.hello.world.R;

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Button aButton;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    aButton = (Button) this.findViewById(R.id.button1);

    aButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(HelloWorldActivity.this, OtherActivity.class);
            startActivity(i);
        }});
}
}

【问题讨论】:

  • 要添加另一个按钮,执行与添加第一个按钮相同的操作。只需在您的布局中添加另一个。并在你的 main.java 文件中膨胀它。
  • 你能告诉我它是哪个代码块吗??

标签: android eclipse button


【解决方案1】:

你应该这样做

// This code is copied from your code as is 
// to have a reference point as well as this is also
// code for adding click listener to button which you
// need to handle click and then do what you want.
// In this case you are launching an Activity
// Block of code you already have to use STARTS
aButton = (Button) this.findViewById(R.id.button1);

aButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(HelloWorldActivity.this, OtherActivity.class);
        startActivity(i);
    }});
// Block of code you already have to use ENDS

// This code is added for newButton which looks similar to above block
Button newButton
newButton = (Button) this.findViewById(R.id.button2);

newButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(HelloWorldActivity.this, AnOtherActivity.class);
        startActivity(i);
    }});

您必须在layout/main.xml 文件中添加另一个button1

【讨论】:

    【解决方案2】:

    您必须知道有两种可能性: 第一个,您必须将“activity.xml”和“Activity.class”分开,在 XML 中您将声明并配置您的按钮,在 .java 中您将分配想要实现的内容。 (有 /out 意图的..)

    第二个onws,你必须在你的.java中声明你的按钮。

    我会告诉你: 活动.xml

    <Button android:id="@+button/test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="First Button"/>
    <Button android:id="@+button/test2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second Button"/>
    

    /////////////////////////////////////// ///////////////

    Activity.java

    ..

    Button first = (Button)findViewById(R.button.test);
    Button second = (Button)findViewById(R.button.test2);
    
    first.setOnclickListener(new OnClickListener() {
        public void onClick(View v) {
            //Do Something guy
        }});
    
    second.setOnclickListener(new OnClickListener() {
        public void onClick(View v) {
            //Do diferent something
        }});
    

    ..

    我希望我能帮上忙。 问候

    【讨论】:

    • 我应该在 //do something guy 和 //do different things 中添加什么?
    【解决方案3】:

    查看Android Developers Blog: UI framework changes in Android 1.6

    特别是底部的“更轻松的点击侦听器”部分。

    “在 Android 1.6 中,这些都不是必需的。你所要做的就是在你的 Activity 中声明一个公共方法来处理点击(该方法必须有一个 View 参数):”

    class MyActivity extends Activity {
        public void myClickHandler(View target) {
            // Do stuff
        } }
    

    “然后从你的 XML 布局中引用这个方法:”

    <Button android:onClick="myClickHandler" />
    

    它只是让代码更简洁。

    【讨论】:

    • 非常感谢,我不知道会这么简单。 :) 但是那个地方说 //do stuff 我应该放什么???
    • 不确定最后一句话是不是玩笑。但如果不是,则取决于您要在应用程序中实现的目标。 “myClickHandler”与被按下的按钮相关联,因此您只需添加用户按下该按钮时所需的任何代码。理想情况下,每个按钮都有一个处理程序。您可以重用您的处理程序并检查作为参数传入的 View,但我不建议您这样做,除非您有这样做的正当理由。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多