如何将editText光标位置与android:hint的位置分开?
这是我的编辑文本:如何将editText光标位置与android:hint的位置分开?,android,xml,user-interface,android-ui,Android,Xml,User Interface,Android Ui,这是我的编辑文本: <EditText android:id="@+id/note" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:
<EditText
android:id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:cursorVisible="false"
android:inputType="textMultiLine"
android:lines="25"
android:scrollHorizontally="false"
android:hint="Type note here" />
提示的位置在中间,我使用android:gravity=“center”实现了这一点。我希望我的文字是从左上角输入的,但由于重力的原因,这并没有发生。有什么方法可以同时做到这两个方面吗?您可以使用
TextWathcer
监视文本输入的时间,然后将TextView
的重力更改为所需的重力:
textEdit.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable editable)
{
}
@Override
public void beforeTextChanged(CharSequence text, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence arg0, int start, int before, int count)
{
textEdit.setGravitiy("to what ever you want");
}
});
在进行这些更改之前,您可能需要检查edittext是否为空。您可以通过编程方式在单击edittext时更改其重力。示例(不确定它是否正确工作,没有测试):
尝试按您提到的方式进行编程更改,但这不起作用。当editText.setGravity()在ContextChange()中完成时,它会起作用,而不是在beforeTextChanged()中完成。无论如何,谢谢!
editText = (EditText) findViewById(R.id.note);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setGravity(Gravity.LEFT);
}
});