2014-06-20 101 views
1

我有一个编辑文本字段。编辑文字已经用不可见符号创建,例如'#' - 不可见符号。当用户点击EditText时,它会在虚拟键盘上显示小字母。Android软键盘打开大写锁定符号后

我希望看到隐形符号后的第一个字母帽和其余的小键盘上。

这怎么办?

+1

'编辑文本已与无形symbol'创建什么意思是? –

+0

我想,他正在初始化#的第一个字母。为什么你不能动态地添加#当你在java端获取值? – Jithu

+0

shylendra, EditText mEditText =(EditText)findViewById(...); mEditText.setText('#'); 用户只能在此符号后输入。 Jithu, 因为#是新款的新象征。它需要更正ParagraphSpans – Dima

回答

2

最后,我创立了解决方案。我通过InputType解决了这个问题。这很糟糕,但没有更好的我没有找到。 在CustomEditText应重写方法onSelectionChanged和改变的inputType有:

public boolean isWasCap = false;// Was capitalisation turn on? 

public void onSelectionChanged(int selStart, int selEnd) { 
    ... 

    setCap(); 
} 

/** 
* Method check is need turn on capitalization 
*/ 
private void setCap(){ 
    int start = getSelectionStart(); 
    int end = getSelectionEnd(); 
    if (start == end && start > 0 
      && getText().charAt(start-1) == INVISIBLE_SYMBOL) { 
     if(isWasCap) // Capitalization is turn on 
      return; 
     setInputMask(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); 
     isWasCap = true; 
    }else if(isWasCap){ 
     isWasCap = false; 
     setInputMask(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); 
    } 
} 

/** 
* Method replace one input type to other input type 
* @param needRemove - mask which will remove 
* @param needSetup - masr which will setup 
*/ 
public void setInputMask(int needRemove, int needSetup){ 
    int mask = getInputType() & ~needRemove; 
    mask = mask | needSetup; 
    setInputType(mask); 
} 

style.xml

<style name="Common.Editor"> 

    <item name="android:inputType">textCapSentences|textMultiLine|textAutoCorrect</item> 
</style> 

main_activity.xml

<com.view.CustomEditText 
     android:id="@+id/edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@style/Common.Editor" 
     /> 
+0

太棒了!正是我需要的东西,像魅力一样工作。不过,我简化了代码很多:int inputType = getInputType(); \t \t \t如果(的SelStart <= 1){ \t \t \t setInputType(的inputType | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); \t \t \t}否则{ \t \t \t setInputType(的inputType&〜InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); \t \t \t} – Ridcully

+0

@Ridcully我必须警告你,这个解决富人的一些缺陷: 1)如果刷卡用户输入文字,这将被粘贴到EditTtext所有文字将维滕资本后者。它需要在TextWatcher内进行监控。 2)将在不同设备上自动完成的工作方式不同。一个不会自动完成这个词,另一个会先自动完成,而另一个会自动完成。 – Dima

+0

感谢您的警告。那么你是如何实现TextWatcher的?这就是为什么你有isWasCap变量? – Ridcully

0

只需在您的EditText上设置android:inputType="textCapSentences"即可。

OR

android:inputType="textCapWords" 
+0

我认为,他正在初始化#的第一个字母。所以这可能无效。 – Jithu

相关问题