2011-01-14 21 views
3

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

} 

回答

0

我最終寫了一個獲取數字的類,並添加了一個微調器。這樣我就可以100%控制發生的事情。

+0

如果您發佈實施方案將非常有用... – moonlightcheese 2011-06-22 18:15:06

0

NumberPicker是標準的API 11

的要根據你的EditText,有很多方法可以做到這一點的重點選擇文本:

  1. 在你layout.xml您可以指定:

    機器人:selectAllOnFocus = 「真」

  2. 在代碼中,你可以添加一個OnFocusChangeListener

    myEditText.setOnFocusChangeListener(新OnFocusChangeListener(){ @覆蓋 公共無效onFocusChange(查看V,布爾hasFocus){ 如果(hasFocus){ ((EditText上)V).selectAll(); } }});

3

實際解決問題的辦法是這樣的:

爲您要使用全選()方法在任何的EditText,應聲明它是作爲BufferType TextView.BufferType.SPANNABLE。這允許您在Spannable項目上執行選擇。有關更多信息,請查找Spannable,EditText和Selection類。這裏是一個編程示例:

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

您還可以使用'android:bufferType'屬性在xml佈局中設置bufferType。

0

雖然您已經選擇了自己的答案,這裏的修復(用C#和Xamarin,但完全可移植到Java):

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); 
} 

我發佈這個人整個問題即將在未來,因爲我很難找到解決方案。