2013-10-22 61 views
0

我有一個多行編輯文本,當用戶開始在其中輸入內容時,我希望它以大寫字母顯示文本,當用戶按「ENTER KEY」時,用小寫字母(小寫)。在edittext中處理大寫和小寫

我該怎麼做? 現在我使用

edittext.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); 

但它不允許我去到下一行上按enter。請幫助

回答

0

下面的代碼:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <EditText 
     android:id="@+id/editTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textCapCharacters" /> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     final EditText edittext = (EditText) findViewById(R.id.editTextView); 
     edittext.setOnKeyListener(new OnKeyListener() { 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       // If the event is a key-down event on the "enter" button 
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
        (keyCode == KeyEvent.KEYCODE_ENTER)) { 
        // Perform action on key press 
        edittext.setText(edittext.getText().toString().toLowerCase()); 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 

} 

如果這是你要找的,請不要忘記標記答案是正確的關閉線程。

+0

1在按ENTER鍵,它不進入下一行, –

+0

2 - 它不會改變文本,而我寫的文字爲小寫,它在我按下回車鍵後將其更改爲小寫 –

+0

使用我寫的代碼作爲起點,現在這是您的工作滿足您的需求。 –

0

我解決它使用文本觀察家

scene.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 

    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 

    } 

    @Override 
    public void afterTextChanged(Editable arg0) { 
    String s=arg0.toString(); 

    if(!s.equals(s.toLowerCase())) { 
     s=s.toUpperCase(); 
     scene.setText(s); 
     } 
    } 
});