Android的按钮navagation与听众的onclick [英] android button navagation with onclick listeners

查看:146
本文介绍了Android的按钮navagation与听众的onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  main.xml中
--------
| BUTTON1 | (按钮preSS)> page1.xml
|按钮2 | (按钮preSS)> page2.xml
|按钮3 | (按钮preSS)> page3.xml
|将Button4 | (按钮preSS)> page4.xml
| button5 | (按钮preSS)> page5.xml
| button6 | (按钮preSS)> page6.xml
-------------------------------------------

在单击一个按钮就进入相应的页面。什么是实现这一目标的最佳/最简单的方法是什么?我想尽一切办法都无济于事,当我尝试的onclick听众。我可以得到一个听者的工作,但是当我试图让多个它弄乱了,我无法弄清楚。
我现在的方法是使用这两个Java文件

activity1.java

 包install.fineline;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;进口android.widget.Button;
进口android.view.View;
进口android.view.View.OnClickListener;公共类活动1扩展活动{按钮Button1的;
按钮Button2的;
按钮按钮3;
按钮将Button4;
按钮Button5;
按钮Button6;公共无效addListenerOnButton(){最后上下文的背景下=这;Button1的=(按钮)findViewById(R.id.autobody);Button1.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意向意图=新意图(背景下,Activity2.class);
        startActivity(意向);    }});按钮2 =(按钮)findViewById(R.id.glass);Button2.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意向意图=新意图(背景下,Activity2.class);
        startActivity(意向);    }});按钮3 =(按钮)findViewById(R.id.wheels);Button3.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意向意图=新意图(背景下,Activity2.class);
        startActivity(意向);    }});将Button4 =(按钮)findViewById(R.id.speedy);Button4.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意向意图=新意图(背景下,Activity2.class);
        startActivity(意向);    }});Button5 =(按钮)findViewById(R.id.sevan);Button5.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){        意向意图=新意图(背景下,Activity2.class);
        startActivity(意向);    }});

Button6 =(按钮)findViewById(R.id.towing);

Button6.setOnClickListener(新OnClickListener(){

  @覆盖
公共无效的onClick(查看为arg0){    意向意图=新意图(背景下,Activity2.class);
    startActivity(意向);}});}}

和这里是我的其他的java文件

activity2.java

    包install.fineline;

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.Button;公共类活性2延伸活动{按钮按钮1;公共无效onCreate1(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.autobody);
}
按钮按钮2;公共无效onCreate2(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.glass);
}
按钮按钮3;公共无效onCreate3(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.wheels);
}
按钮将Button4;公共无效onCreate4(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.speedy);
}
按钮button5;公共无效onCreate5(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.sevan);
}按钮button6;公共无效onCreate6(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.towing);
}}


解决方案

您应该定义为每个页面的一个活动。所以,完全7活动包括主。在主要活动,确定单onClickListener来处理所有的点击次数。

 类ClickListener实现View.OnClickListener {
    无效的onClick(视图v){
       开关(v.getId()){
        案例R.id.btn_1:
             ...
             打破;
        ...
       }
    }

设置此监听到所有的6个按键。

main.xml
--------
|button1| (button press)>page1.xml
|button2| (button press)>page2.xml
|button3| (button press)>page3.xml
|button4| (button press)>page4.xml
|button5| (button press)>page5.xml
|button6| (button press)>page6.xml
-------------------------------------------

When a button is clicked it goes to a corresponding page. What is the best/easiest way to implement this? I have tried everything to no avail when I try onclick listeners. I can get one listener to work but when I try to get more than one it messes up and I can't figure it out. My current method is using these 2 java files
activity1.java

package install.fineline;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

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 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, 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);   

}

});

}}

and here is my other java file

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 onCreate4(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);
}}

解决方案

You should define one activity for each of the pages. So totally 7 activities including the main. In main activity, define single onClickListener to handle all clicks.

class ClickListener implements View.OnClickListener {
    void onClick(View v) {
       switch(v.getId()) {
        case R.id.btn_1:
             ...
             break;
        ...
       }
    }

Set this listener to all your 6 buttons.

这篇关于Android的按钮navagation与听众的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆