在一个活动android应用程序中创建多个函数

时间:2012-10-23 12:47:46

标签: java android xml

问题1: 我有2个活动。我想知道如何优化它。我可以用多个监听器创建2个活动。或者为每个按钮创建多个java文件(onclick listener) 问题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 =(按钮)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);
}}

1 个答案:

答案 0 :(得分:0)

你的回答肯定是正确的。但永远不会调用onCreate1onCreate2等等。相反,试试这个:

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()方法。

我认为您应该查看一些basic Java tutorials以了解更多语法,然后再查看Android tutorials。这有助于您了解Activity2中的代码无法按照您认为应该工作的方式运行的原因,并让您更好地了解如何处理单独的活动。没有冒犯:)我们都在学习。

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