【问题标题】:Edittext first letter not auto capitalizingEdittext第一个字母不自动大写
【发布时间】:2016-08-01 12:17:30
【问题描述】:

您好,我的编辑文本遇到问题。

Edittext 的 xml 粘贴在下面:

 <EditText
                android:id="@+id/edttxt_description_taskdescription"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/padding_large"
                android:layout_marginTop="@dimen/margin_.5x"
                android:background="@color/white"
                android:lines="2"
                android:gravity="top"
                android:hint="@string/activity_task_description_name_hint"
                android:imeOptions="actionDone"
                android:singleLine="false"
                android:inputType="textMultiLine|textCapSentences"
                android:maxLength="85"
                android:textSize="@dimen/text_16pixels" />

问题:我想将 EditText 的第一个字母自动大写,但它没有发生。请帮忙!

注意:我想要一个多行 EditText。

【问题讨论】:

  • 实际上这段代码将每行开头的单词都设为大写字母。请您验证一下。是否需要将一行中的每个单词都设为大写字母?
  • 尝试在 inputType 中添加 textCapWords,如 android:inputType="textMultiLine|textCapWords|textCapSentences"。
  • 我刚刚测试了你的代码,它对我有用。您是否在代码中的某处以编程方式修改您的 EditText?
  • @babadaba 你在哪个 API 版本中测试这个?
  • @MujammilAhamed 不,我只想将段落的第一个字母设为大写。

标签: android android-layout android-edittext capitalization


【解决方案1】:

您应该将 inputType textCapSentences 更改为 textCapWords。

<EditText
            android:id="@+id/edttxt_description_taskdescription"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/padding_large"
            android:layout_marginTop="@dimen/margin_.5x"
            android:background="@color/white"
            android:lines="2"
            android:gravity="top"
            android:hint="@string/activity_task_description_name_hint"
            android:imeOptions="actionDone"
            android:singleLine="false"
            android:inputType="textMultiLine|textCapWords"
            android:maxLength="85"
            android:textSize="@dimen/text_16pixels" />

【讨论】:

    【解决方案2】:

    您可以在制作大写句子的活动中使用此代码。在每个句子中,第一个单词的字母大写,新的句子表示在点(。)和空格之后。

    for example:
     i/p---> hi hello. hi hello    
      o/p---> Hi hello. Hi Hello
    

    使用此代码

     EditText assignmentName=(EditText) findViewById(R.id.assignmentNameId);
         String sassignmentName = assignmentName.getText().toString();
         char[] assignment=sassignmentName.toCharArray();
                    char[] assignment1=new char[assignment.length];
                    int  count=0;
                    for(int i=0;i<=assignment.length-1;i++)
                    {
                        if(i==0 && Character.isLowerCase(assignment[i]) ) {
                            char first = assignment[i];
                            char first1 = Character.toUpperCase(first);
                            assignment1[count] = first1;
                            count++;
                        }else if(Character.isWhitespace(assignment[i-1]) &&  assignment[i-2]=='.'  && Character.isLowerCase(assignment[i]))
                        {
                            char first = assignment[i];
                            char first1 = Character.toUpperCase(first);
                            assignment1[count] = first1;
                            count++;
                        }
                        else {
                            assignment1[count]=assignment[i];
                            count++;
                        }
                    }
                    String UpperCaseassignmentName=String.valueOf(assignment1);
    

    【讨论】:

      【解决方案3】:
      android:inputType="textCapSentences"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 2018-07-08
        • 2017-03-30
        • 2020-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多