3

我在textinputlayout中有一個編輯文本。我試圖將必填字段符號(紅色星號)設置爲edittext。我已將提示設置爲編輯文本。我的代碼如下。在文本輸入框中輸入編輯文本(紅色星號)的必填符號

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="16dp" 
     android:layout_marginTop="16dp" 
     android:orientation="horizontal"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:text=" * " 
      android:layout_gravity="center_vertical" 

      android:textColor="#ff0000" 
      android:textSize="15sp" /> 
     <android.support.design.widget.TextInputLayout 

      android:id="@+id/tilEngNo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      > 

       <EditText 
       android:id="@+id/engineno" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginLeft="15dp" 
      android:layout_marginRight="16dp" 
       android:hint="Engine No" /> 
     </android.support.design.widget.TextInputLayout> 
    </LinearLayout> 

這給了我textinputlayout外部的星號符號。所以當我開始在編輯文本中輸入時,提示會浮起來,但不會顯示星號。 我的要求是讓星號符號的行爲與edittext的提示相同。什麼是最好的方法? 任何幫助表示讚賞。在此先感謝

回答

0

您可以使用一個小技巧來做到這一點,即您必須更改在Java代碼中調用。例如:

//Set OnFocusChangeListener for EditText 
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View view, boolean hasFocus) { 
       //astriskText is Object of your textview contains * as text 
       if (hasFocus) { 
        //if edittext has focus then move it to top 
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) astriskText.getLayoutParams(); 
        lp.gravity= Gravity.TOP; 
        astriskText.setLayoutParams(lp); 
       } else if(edittext.getText().toString().length()==0){ 
        //else check if edittext is empty then move it back to center 
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) astriskText.getLayoutParams(); 
        lp.gravity= Gravity.CENTER_VERTICAL; 
        astriskText.setLayoutParams(lp); 
       } 
      } 
     }); 
2

假設您有EditTexts爲強制值。你希望提示是一個紅色星號,後面是淡灰色的文本。

的EditTexts後你有解釋:TextView的紅色星號後面的文本「=字段是必須的」

在我來說,我用這個這個:

showEditTextsAsMandatory (joinEmailET, joinNameET, passwordET, confirmPasswordET); 
showTextViewsAsMandatory ((TextView) findViewById (R.id.requiredText)); 

public void showEditTextsAsMandatory (EditText... ets) 
{ 
    for (EditText et : ets) 
    { 
     String hint = et.getHint().toString(); 

     et.setHint (Html.fromHtml ("<font color=\"#ff0000\">" + "* " + "</font>" + hint)); 
    } 
} 

public void showTextViewsAsMandatory (TextView... tvs) 
{ 
    for (TextView tv : tvs) 
    { 
     String text = tv.getText().toString(); 

     tv.setText (Html.fromHtml ("<font color=\"#ff0000\">" + "* " + "</font>" + text)); 
    } 
} 
0

嘗試用您的加盟TextInputLayout提示跨區後綴包裹在HTML:

public void setRequired(boolean required) { 

    componentField.setHint(
    TextUtils.concat(
     componentField.getHint(), 
     Html.fromHtml(
     getContext().getString(
      R.string.required_asterisk)))); 
} 

Asterisk的代碼應該存儲在正確的逃逸機器人的strings.xml:

<string name = "required_asterisk"><![CDATA[<font color=\'#cc0029\'>&nbsp*</font>]]></string> 
相關問題