Android 选择警报弹出窗口上的所有文本selectAll()

Android 选择警报弹出窗口上的所有文本selectAll(),android,keyboard,Android,Keyboard,我使用selectAll()函数,但它似乎什么都不做。有没有办法突出显示文本,这样他们就不必退格然后重新键入 void GetQuantity() { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Quantity"); alert.setMessage("Enter Quantity"); final EditText input = new EditTe

我使用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();

}

最后我写了一个获取数字的类,并添加了一个微调器。这样我就可以100%地控制正在发生的事情。

是API 11的标准

要根据焦点选择文本,有以下几种方法:

  • 在layout.xml中,可以指定:

    android:selectAllOnFocus=“true”

  • 在代码中,您可以添加:

    myEditText.setOnFocusChangeListener(新的OnFocusChangeListener(){ @凌驾 public void onFocusChange(视图v,布尔hasFocus){ 如果(hasFocus){ ((编辑文本)v.选择全部(); } }});


  • 问题的实际解决方案是:

    对于要在其上使用selectAll()方法的任何EditText,应将其BufferType声明为TextView.BufferType.Spanable。这允许您对可扩展项执行选择。有关更多信息,请查阅Spanable、EditText和Selection类。以下是一个编程示例:

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

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

    虽然您已经选择了自己的答案,但下面是修复方法(使用C#和Xamarin,但完全可移植到Java):

    private void InitFocus()
    {
    Java.Lang.Runnable rable=新的Java.Lang.Runnable(()=>
    {
    EditText maleCount=FindViewById(Resource.Id.txtMaleCount);
    RequestFocus();
    maleCount.SelectAll();
    });
    新处理程序().Post(可拆分);
    }
    
    我为将来遇到这个问题的人发这篇文章,因为我很难找到解决方案

    private void InitFocus()
    {
        Java.Lang.Runnable rable = new Java.Lang.Runnable(()=> 
        {
            EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
            maleCount.RequestFocus();
            maleCount.SelectAll();
        });
        new Handler().Post(rable);
    }