简体   繁体   English

如何将 integer 变量从活动传递到片段?

[英]How can I pass an integer variable from an activity to a fragment?

I have an activity with two variables我有一个包含两个变量的活动

int cifracero and int cifrauno int cifrauno 和 int cifrauno

I want to pass those variables to a fragment to be shown in some TextViews as the result of the multiplication made in the previous screen...like a "Congratulations, the multiplication was..."我想将这些变量传递给一个片段,以便在某些 TextViews 中显示,作为上一个屏幕中乘法的结果……就像“恭喜,乘法是……”

I need to do something like a bundle to pass the variables as arguments of the second screen, in this case a fragment.我需要做一些类似捆绑的事情来将变量作为第二个屏幕的 arguments 传递,在本例中是一个片段。

And then I have a third variable resultado which is the result of the multiplication of cifracero and cifrauno然后我有第三个变量 resultado,它是 cifracero 和 cifrauno 相乘的结果

This is the activity code这是活动代码

    package com.example.aprendelastablasdemultiplicar;

    import androidx.activity.ComponentActivity;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.ListFragment;

    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    import org.w3c.dom.Text;

    public class quizingresar extends ComponentActivity {

        private EditText cifracero;
        private EditText cifrauno;
        private TextView resultado;
        private Button calcular;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_quizingresar);


  
            cifracero = (EditText) findViewById(R.id.cifracero);
            cifrauno = (EditText) findViewById(R.id.cifrauno);
            resultado = (TextView) findViewById(R.id.resultado);
            calcular = (Button)findViewById(R.id.calcular);

             TextWatcher textWatcher = new TextWatcher() {
                 @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

                 @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    int multiplicationResult = multiplicarValores();
                resultado.setText(Integer.toString(multiplicationResult));
                 }

                @Override
                public void afterTextChanged(Editable s) {

        }


    };

            cifracero.addTextChangedListener(textWatcher);
            cifrauno.addTextChangedListener(textWatcher);

        calcular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCongratulationsCalcular();

    }
});
}


        private int multiplicarValores() {
            final String strnumber0 = cifracero.getText().toString();
            final String strnumber1 = cifrauno.getText().toString();
            int number0 = 0;
            int number1 = 0;

            try {
                number0 = Integer.parseInt(strnumber0);
    } catch (NumberFormatException e) {

    }
            try {
                number1 = Integer.parseInt(strnumber1);
    } catch (NumberFormatException e) {

    }

             return number0 * number1;
}

    public void openCongratulationsCalcular(){
        Intent intent = new Intent(this, congratulationscalcular.class);
        startActivity(intent);

} }

} }

I need to recover the variables from the previous screen to be shown in the new fragment screen我需要从上一个屏幕中恢复变量以显示在新的片段屏幕中

You can have both of your fragments share the data between them by having them both under the same viewmodel, as shown here .您可以让两个片段在同一视图模型下共享它们之间的数据,如此处所示

If you are going to be swapping screens, I think a very efficient solution would be to pass the desired data from one (screen) fragment to another using attributes in your Navigation Graph.如果您要交换屏幕,我认为一个非常有效的解决方案是使用导航图中的属性将所需数据从一个(屏幕)片段传递到另一个片段。 Shown here . 显示在这里

Take a look at Data-Binding as well if you'd like, might be useful.如果您愿意,也可以查看Data-Binding ,可能会有用。

Intent intent = new Intent(this, DisplayMessageActivity.class);
        String result = resultado.getText().toString();
        intent.putExtra("EXTRA_MESSAGE", result);
        startActivity(intent);

to get the message that was passed by the first activity获取第一个活动传递的消息

Intent intent = getIntent();
String message = intent.getStringExtra("EXTRA_MESSAGE");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM