按钮一应该针对第一页

时间:2014-06-29 23:17:09

标签: java android xml eclipse

我有一个页面,有两个按钮来"两个"其他页面,事情是,按钮1和按钮2通向第二页,思想按钮1应该通向第一页,按钮2应该通向第二页,这里是java文件,我不知道怎么说呢!

ActiveMain.java

package com.d.di;

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 MainActivity extends Activity {

    Button button1;
    Button button2;

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

    public void addListenerOnButton() {
        final Context context = this;
        button1 = (Button) findViewById(R.id.abus);
        button2 = (Button) findViewById(R.id.weoff);


         button1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

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

                }

            });

            button2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    Intent intent = new Intent(context, PageTwo.class);
                    // you made a mistake here you called the PageOne again here while you should call the second. 

                    startActivity(intent);

                }

            });



    }

}

PageOne.java

package com.d.di;

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

public class PageOne extends Activity {

    Button button1;


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

PageTwo

package com.d.di;

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

public class PageTwo extends Activity {

    Button button2;


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

1 个答案:

答案 0 :(得分:0)

 button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

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

        }

    });

    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, PageTWO.class); // you made a  
 mistake here you called the PageOne again here while you should call the second. 

            startActivity(intent);

        }

    });