簡體   English   中英

在活動之間傳遞信息

[英]Pass information between activities

我正在做一個必須對計步器進行編程的項目。 計步器將通過按鈕運行,您必須告知步長。 我進行了一項主要活動,讓您在匿名和另一個可以注冊之間進行選擇。 當我注冊或匿名時,應用程序就會起作用,並且步長會很好地傳遞給第三個活動,在該活動中,您可以按一個按鈕並增加完成的步數和完成的測量。 在此活動中,還有另一個按鈕可配置步驟的長度,我想將所有信息傳遞給匿名活動,以僅更改步驟的長度,而我傳遞所有信息。 我使用了方法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,所以我不知道自己在做什么錯。 如果您能幫助我,我將不勝感激。 另外,我認為這與捆綁銷售有關。

將所有數據添加到捆綁中,然后僅檢查bundle!= null –作者:米蘭·潘蘇里亞(Milan Pansuriya)

我不知道使用捆綁包將所有數據和putExtra傳遞到我的意圖之間的區別,但這對我有用。 謝謝米蘭潘蘇里亞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM