0

问题 1:我有 2 个活动。我想知道如何优化它。我可以使用多个侦听器创建 2 个活动。或者为每个按钮创建多个 java 文件(onclick 监听器) 问题 2:我尝试在一个 java 中创建多个监听器,但只能让一个按钮工作。一个java文件中多个监听器的语法是什么?这是我的*更新代码:现在的问题是无论单击什么按钮都会导致相同的页面。我认为问题在这些括号 [-{}-] activity1.java 中的文本中指出

package install.fineline;

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

public class Activity1 extends Activity {

Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;
[-{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fineline);
addListenerOnButton();
}
}-]

public void addListenerOnButton() {

final Context context = this;

Button1 = (Button) findViewById(R.id.autobody);

Button1.setOnClickListener(new OnClickListener() {


    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button2 = (Button) findViewById(R.id.glass);

Button2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button3 = (Button) findViewById(R.id.wheels);

Button3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button4 = (Button) findViewById(R.id.speedy);

Button4.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button5 = (Button) findViewById(R.id.sevan);

Button5.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button6 = (Button) findViewById(R.id.towing);

Button6.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

    Intent intent = new Intent(context, Activity2.class);
    startActivity(intent);   

}

});

}}

activity2.java

package install.fineline;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Activity2 extends Activity {

Button button1;

public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autobody);
}
Button button2;

public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
Button button3;

public void onCreate3(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wheels);
}
Button button4;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedy);
}
Button button5;

public void onCreate5(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sevan);
}

Button button6;

public void onCreate6(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.towing);
}}
4

1 回答 1

0

你的答案绝对是正确的。但是onCreate1, onCreate2, 等永远不会被调用。相反,试试这个:

package install.fineline;

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

public class Activity1 extends Activity {

Button button1;
Button button2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fineline);
    addListenerOnButton();
}

public void addListenerOnButton() {

    final Context context = this;

    button1 = (Button) findViewById(R.id.autobody);

    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, AutoBodyActivity.class);
            startActivity(intent);   

        }

    });

    button2 = (Button) findViewById(R.id.glass);
    button2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        Intent intent = new Intent(context, GlassActivity.class);
        startActivity(intent);   
    }
});
}

并为您想要提出的每个布局单独制作Activity(请注意,在上面的代码中,我引用了AutoBodyActivityGlassActivity不是Activity2):

package install.fineline;

import android.app.Activity;
import android.os.Bundle;

public class AutoBodyActivity extends Activity {

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autobody);
    }
}

和:

package install.fineline;

import android.app.Activity;
import android.os.Bundle;

public class GlassActivity extends Activity {

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.glass);
    }
}

它在创建Activity. onCreate()创建时会自动调用该方法Activity,但onCreate1()从未调用过该方法。

我认为您应该查看一些基本的 Java 教程以了解更多的语法,然后再学习Android 教程。这将帮助您理解为什么您的代码Activity2没有按照您认为的方式工作,并让您更好地了解如何处理单独的活动。无意冒犯:) 我们都在学习。

希望我的回答对你有所帮助。

于 2012-10-23T12:53:22.713 回答