在活动之间传递信息

时间:2018-12-26 12:53:22

标签: java android android-studio bundle

我正在做一个必须编写计步器的项目。计步器将通过按钮运行,您必须告知步长。我进行了一项主要活动,让您在匿名和另一个可以注册之间进行选择。当我注册或匿名时,应用程序就会起作用,并且步长会很好地传递给第三个活动,在该活动中,您可以按一个按钮并增加完成的步数和完成的测量。在此活动中,还有另一个按钮可配置步骤的长度,我想将所有信息传递给匿名活动,以仅更改步骤的长度,而我传递所有信息。我使用了方法putExtra()和getIntent()。getExtra()。getString(),但仅适用于将主要活动转到注册/匿名活动,然后转到计步器活动,但是当我想配置长度时应用停止的步骤。

这是我的匿名活动代码:

if(this.getIntent().hasExtra("name")){
        names=this.getIntent().getExtras().getString("name");
    }else{
        names="";
    }

    if(this.getIntent().hasExtra("user")){
        userName=this.getIntent().getExtras().getString("user");
    }else{
        userName="";
    }

    if(this.getIntent().hasExtra("pass")){
        password=this.getIntent().getExtras().getString("pass");
    }else{
        password="";
    }

    if(this.getIntent().hasExtra("feetBefore")){
        footBefore=this.getIntent().getExtras().getString("feetBefore");
    }else{
        footBefore="0";
    }

    if(this.getIntent().hasExtra("steps")){
        stepsDone=this.getIntent().getExtras().getString("steps");
    }else{
        stepsDone="0";
    }


    Button continueBtn = findViewById(R.id.continueAnonymous);
    foot =  findViewById(R.id.lengthFeetAnonymous);

    continueBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String footSize = String.valueOf(foot.getText());
            if(!footSize.equals("")) {
                Intent mainAnonymous = new Intent(getApplicationContext(), Main5Activity.class);
                mainAnonymous.putExtra("name", names);
                mainAnonymous.putExtra("user", userName);
                mainAnonymous.putExtra("pass", password);
                mainAnonymous.putExtra("feet", footSize);
                mainAnonymous.putExtra("steps", stepsDone);
                mainAnonymous.putExtra("feetBefore", footBefore);
                startActivity(mainAnonymous);
                finish() ;
            }else{
                Toast.makeText(Main4Activity.this, "You have to complete all the backets.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

这是我的计步器活动代码:

Bundle parameters = this.getIntent().getExtras();
    if(parameters != null){
        name = parameters.getString("name");
        username = parameters.getString("user");
        password = parameters.getString("pass");
        String foot = parameters.getString("feet");
        String footBefore = parameters.getString("feetBefore");
        String stepsDone = parameters.getString("steps");
        if(stepsDone!=null) cont = Integer.parseInt(stepsDone);
        else cont =0;
        if(footBefore!=null)feetBefore = Integer.parseInt(footBefore);
        else feetBefore =0;
        if(foot !=null)feet = Float.parseFloat(foot)/100;
        else feet = (float) 0.43;
        cont2 = cont*feetBefore;
    }else {
        name = "";
        username = "";
        password = "";
        feet = (float) 0.43;
    }

    increase = findViewById(R.id.increaseStep);
    configuration = findViewById(R.id.confBtn);
    saveMain = findViewById(R.id.saveBtnMain);
    resume = findViewById(R.id.resumBtn);

    final TextView steps = findViewById(R.id.stepCounter);
    final TextView km = findViewById(R.id.kilometerCounter);
    steps.setText(String.format(Locale.ENGLISH,"%d Steps",cont));
    String aux =String.format(Locale.ENGLISH,"%.2f Meters", cont2);
    km.setText(aux);

    increase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cont++;
            steps.setText(String.format(Locale.ENGLISH,"%d Steps",cont));
            cont2 += feet;
            String aux =String.format(Locale.ENGLISH,"%.2f Meters", cont2);
            km.setText(aux);
        }
    });

    configuration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent conf = new Intent(getApplicationContext(), Main4Activity.class);
            conf.putExtra("name", name);
            conf.putExtra("user", username);
            conf.putExtra("pass", password);
            String aux2 = String.valueOf(cont);
            conf.putExtra("steps", aux2);
            float aux4 =feet*100;
            String aux3 = String.valueOf(aux4);
            conf.putExtra("feetBefore", aux3);
            startActivity(conf);
            finish() ;
        }
    });
}

我昨天开始学习android,所以我不知道自己在做什么错。如果您能帮助我,我将不胜感激。另外,我认为这与捆绑销售有关。

1 个答案:

答案 0 :(得分:0)

将所有数据添加到捆绑中,并仅检查是否捆绑!= null – Milan Pansuriya

我不知道使用捆绑包传递所有数据和将putExtra传递到我的意图之间的区别,但这对我有用。谢谢米兰潘苏里亚。