我的问题类似于this ,但是在安卓中。我正在尝试从 EditText 获取输入,然后单击按钮将最终结果显示到 TextView 中。
我在尝试调整这三个答案时遇到了麻烦,OP说这三个答案都有效。
如何从 TextView 中获取值并将结果显示在 TextView 中?
下面的方法似乎有效,但输出是 000,我猜是因为 arrayList 中没有存储任何内容
input = (Edittext)findViewById(R.id.et_input);
output = (TextView)findViewById(R.id.tv_output);
ArrayList<Integer> myList = new ArrayList<Integer>();
Scanner val = new Scanner(System.in); //can't figure out how to get user input from editText
int x = 0;
for (int i = 0; i < 3; i++) {
x = val.nextInt();
myList.add(x);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
myList.sort(null);
}
for (int i = myList.size() - 1; i >= 0; i--) {
answer += myList.get(i).toString();
}
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
output.setText(answer);
}
});
最佳答案
你可能会做这样的事情:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// use textview to display the result
output = (TextView)findViewByIf(R.id.textview1);
// use a button to activate sorting once you have the edittexts filled
sortNumbersButton = (Button)findViewById(R.id.button);
// use EditText(s) to get values from user
input = (EditText)findViewById(R.id.edittext1);
// set a listener for a button so that we can activate sorting
sortNumbersButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
// use a string array to store string values from input
string[] strings = input.getText().toString().split(" ");
// use the editText getText() method to pull values from UI
int[] intArray = new int[] {Integer.parseInt(strings[0]),
Integer.parseInt(strings[1]),
Integer.parseInt(strings[2])};
// sort the values in the array in ascending order
Arrays.sort(intArray);
// use stringbuilder to append string values for output
StringBuilder sb = new StringBuilder();
// loop backwards since it sorts in ascending order
for (int i = 2; i > -1; i--) {
sb.append(intArray[i]);
}
// set the textview value of the sorted integers! and done
output.setText(sb.toString());
}
});
}
关于java - 打印给定数字中最大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51062761/