2016-09-30 20 views
0

android editText的高度是wrap_content xml和tint字符串是超過2行。當我只用幾個字來調用setText時,EditText的行數似乎是2行,但不是1行。當我有2行色調字符串時,如何使wrap_content工作。我如何使wrap_content工作時,我有一個2行色調字符串

與色彩感顯示畫面設置文本

enter image description here

我的XML後

enter image description here

圖片:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    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="com.example.lemon.edittexttintdemo.MainActivity"> 

    <EditText 
     android:id="@+id/edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="top" 
     android:hint="this is a very long tint string which is more than one line"/> 

    <Button 
     android:id="@+id/button" 
     android:layout_below="@id/edit_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="click"/> 

    <EditText 
     android:layout_below="@id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="one line text example"/> 

</RelativeLayout> 

我的Java代碼:

public class MainActivity extends AppCompatActivity { 

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

     final EditText editText = (EditText) findViewById(R.id.edit_text); 
     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       editText.setText("In fact there is only one line"); 
      } 
     }); 
    } 
} 

回答

1

只是把線的editText.setText( 「線」)以上;

editText.setHint(「」);

+0

哦〜,非常感謝,您的回答是我想要的 – leimenghao

+0

歡迎@leimenghao –

+0

這將刪除提示。如果用戶編輯並使edittext爲空。它不會再顯示提示。 –

0

試試這個:

final EditText t = findViewById(someName); 
    t.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      t.setSingleLine(t.getText().toString().length() > 0); 
     } 
    }); 
+0

你可以使用'onTextChanged'而不是'afterTextChanged'。 –

+0

我希望wrap_content工作意味着用戶可以無限制地輸入單詞,所以行數可能是3,4或1.但是,您的解決方案將行數限制爲1. 感謝所有相同的 – leimenghao

0

嘿試試這個解決方案。

​​

快樂編碼。

+0

是的,我已經解決了我的問題,就像你說的。 @vrundpurohit – leimenghao

+0

很高興幫助。你可以提高我的答案壽。 ,p –

相關問題