选择警报弹出窗口中的所有文本selectAll()(Select all text on alert popup selectAll())

我使用selectAll()函数但它似乎根本不做任何事情。 有没有办法让文本突出显示,以便它们不必退格然后重新键入?

void GetQuantity() { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Quantity"); alert.setMessage("Enter Quantity"); final EditText input = new EditText(this); alert.setView(input); input.setText("1"); // android:selectAllOnFocus="true" input.setInputType(InputType.TYPE_CLASS_NUMBER); input.selectAll(); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Quantity =Double.parseDouble( input.getText().toString()); btnQuan.setText(input.getText().toString()); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // do nothing } }); alert.show(); }

I use the selectAll() function but it doesnt seem to do anything at all. Is there a way to have the text highlighted so they do not have to backspace and then retype?

void GetQuantity() { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Quantity"); alert.setMessage("Enter Quantity"); final EditText input = new EditText(this); alert.setView(input); input.setText("1"); // android:selectAllOnFocus="true" input.setInputType(InputType.TYPE_CLASS_NUMBER); input.selectAll(); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Quantity =Double.parseDouble( input.getText().toString()); btnQuan.setText(input.getText().toString()); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // do nothing } }); alert.show(); }

最满意答案

这个问题的实际解决方案是这样的:

对于要使用selectAll()方法的任何EditText,应将其BufferType声明为TextView.BufferType.SPANNABLE。 这允许您对Spannable项目执行选择。 有关更多信息,请查找Spannable,EditText和Selection类。 这是一个程序化的例子:

myEditText.setText("mytext", TextView.BufferType.SPANNABLE); myEditText.selectAll();

您还可以使用'android:bufferType'属性在xml布局中设置bufferType。

I wound up writing a class for getting numbers and added a spinner too. that way I have 100% control of what is happening.

更多推荐