我正在编写一个应用程序来对数字进行排序并显示排序过程 输入排序后,将显示一个新按钮,用于在新 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/