java - 如何在另一个 Activity 的 TextView 中调用方法的输出?

标签 java android android-studio android-activity

我正在编写一个应用程序来对数字进行排序并显示排序过程 输入排序后,将显示一个新按钮,用于在新 Activity 中显示选择排序步骤

[SelectionSort Activity 1

我希望 SelectionSort 类中函数 SelectionSortMethod 的输出显示在新 Activity activity_Ssteps

SelectionSort.java:

public class SelectionSort extends AppCompatActivity {

    EditText input;
    EditText output;
    Button Ssteps ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selection_sort);
        input=findViewById(R.id.input);
        output=findViewById(R.id.output);
         Ssteps = findViewById(R.id.steps);

        Ssteps.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                Intent s = new Intent(SelectionSort.this, com.example.sorted.Ssteps.class);

                startActivityForResult(s, 1);
            }
        });}

    public void sortButtonPressed(View view){
            String[] numberList = input.getText().toString().split(","); 

            Integer[] numbers = new Integer[numberList.length];
            for (int i = 0; i < numberList.length; i++) {

                numbers[i] = Integer.parseInt(numberList[i]);
            }

            SelectionSortmethod(numbers); 
            output.setText(Arrays.toString(numbers));

// if button "sort " is pressed , the button "view steps "will be displayed
            Ssteps.setVisibility(View.VISIBLE);
        }
    }

    public static void SelectionSortmethod (Integer[]  arr)
    {
        // some code for sorting and showing the steps
}

Ssteps.java:

public class Ssteps extends AppCompatActivity {
    TextView steps_text ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ssteps);
        setTitle("selection sort steps ");
        steps_text =findViewById(R.id.Stepstextview);
    }
}

最佳答案

您可以像这样使用intent.extras:

   Intent s = new Intent(SelectionSort.this, com.example.sorted.Ssteps.class);
   s.putExtra("AnyID",YOURDATA);
   startActivity(s);

然后在您的 Ssteps.class 中您可以使用以下方式获取数据 id 像这样:

String ss = getIntent.getExtra("AnyID"); //the id is the same as the other one above
steps_text.settext(ss);

关于java - 如何在另一个 Activity 的 TextView 中调用方法的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60553592/

相关文章:

java - 参数化接口(interface)可以是子接口(interface)吗?

java - 自定义对话与键盘 Android

Android:选项卡切换 - 如何在选项卡之间切换时更新显示?

android - 无法在 Android Studio : Unable to load native library 中使用 CMake 链接库

android - Play商店上的PWA2APK仍然在顶部显示网址栏

android - Android同步失败:找不到项目

java - 如何 "update"一个现有的命名实体识别模型——而不是从头开始创建?

java - 在 Eclipse 中使用文件