如何将大写字母转换为小写?

时间:2015-05-26 05:18:05

标签: android android-edittext uppercase

我正在创建一个应用程序,在我的登录页面中我能够输入大写和小写字母,我想要的是如果用户输入大写字母它应该转换为小写,简而言之我不想让用户输入大写字母

      <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/f_name"
        android:gravity="center"
        android:hint="EMAIL"
        android:inputType="textEmailAddress"
        android:textColor="#676767"
        android:textSize="20sp" 

        />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@id/email"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/f_name"
        android:gravity="center"
        android:hint="PASSWORD"
        android:inputType="textPassword"
        android:textColor="#676767"
        android:textSize="20sp"

         />

代码

   etemail = (EditText) findViewById(R.id.email);

    etemail.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE);

5 个答案:

答案 0 :(得分:10)

在XML中,如果要限制用户仅输入小写,请为edittext添加此项。

android:digits="abcdefghijklmnopqrstuvwxyz "

OR

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

如果您还想允许输入数字。

答案 1 :(得分:4)

    EditText edit = (EditText)findViewById(R.id.email);

    String input;

    input = edit.getText().toString();
    input = input.toLowerCase(); //converts the string to lowercase
       or 
    input = input.toUpperCase(); //converts the string to uppercase

答案 2 :(得分:0)

试试这个:

<form action="/dosomething.htm" method="GET" onsubmit="return store(this)">
     [...]
     <input type="submit" value="Go">
   </form>

<script type="text/javascript">
function store() {

        var newReg = /^[A-Z]+[a-z]+[0-9]+$/

        if (usrnm.value.match(newReg)) {
            //next action here
            return true;
        } else {
            usrnm_check.innerHTML = "Username should have alphabets and numbers";
            return false;
        }
    }
</script>

答案 3 :(得分:0)

我可以建议你一个小技巧,你可以在编辑文本中添加一个textchangelistener,并在更改文本后立即对每个字母进行去大写。

   Field1 = (EditText) findViewById(R.id.email);

   Field1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) 
   {
      Field1.setText(s.toString().toLowerCase());
    }

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) 
   {
      Field1.setText(s.toString().toLowerCase());
   }
  });

答案 4 :(得分:0)

您需要添加此代码,以便在用户输入时将EditText转换为小写。

//Add this line to the onCreate method
editText.addTextChangedListener(editTextWatcher);

//Create the TextWatcher as a class variable (outside methods).
private final TextWatcher editTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String s = editable.toString();
        if (!s.equals(s.toLowerCase())) {
            s = s.toLowerCase();
            R.id.editText.setText(s);
            R.id.editText.setSelection(R.id.editText.getText().toString().length());
        }
    }
};