我的问题类似于this,但在android中。我试图从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);
}
});
答案 0 :(得分:2)
您可能可以执行以下操作:
@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());
}
});
}
答案 1 :(得分:1)
这可以解决问题
在EditText中添加数字,并在每个数字之间用“”(空格)
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] digits = input.getText().toString().split(" ");
ArrayList<Integer> digitArray = new ArrayList<>();
for (String digit : digits) {
if (TextUtils.isDigitsOnly(digit))
digitArray.add(Integer.parseInt(digit));
}
Collections.sort(digitArray);
StringBuilder largestPossibleNumber = new StringBuilder();
for (int i = digitArray.size(); i > 0; i--) {
largestPossibleNumber.append(digitArray.get(i - 1));
}
output.setText(largestPossibleNumber.toString());
}
});
不需要
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) {
String enteredText = input.getText().toString();
ArrayList<Integer> digitArray = new ArrayList<>();
for(int i = 0 ; i< enteredText.length(); i++) {
if (TextUtils.isDigitsOnly(String.valueOf(enteredText.charAt(i))))
digitArray.add(Integer.parseInt(String.valueOf(enteredText.charAt(i))));
}
Collections.sort(digitArray);
StringBuilder largestPossibleNumber = new StringBuilder();
for (int i = digitArray.size(); i > 0; i--) {
largestPossibleNumber.append(digitArray.get(i - 1));
}
output.setText(largestPossibleNumber.toString());
}
});