<EditText
android:id="@+id/rrValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical|right"
android:textColor="@color/black"
android:textSize="15sp"
android:singleLine="true"
android:background="@null"
android:hint="why the cursor always on my left?"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"/>
最佳答案
为此,在EditText的正后添加一个新的TextView并将其文本设置为提示的文本。这是该代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:id="@+id/rrValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical|right"
android:textColor="@color/black"
android:textSize="15sp"
android:singleLine="true"
android:background="#a2b3c4"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:paddingRight="7dp"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="15sp"
android:text="why the cursor always on my left?"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:paddingRight="7dp"
/>
</RelativeLayout>
然后在Activity类中,需要添加TextChangedListener并隐藏文本视图,因为Edit Text中没有文本:
EditText rrValue = (EditText)findViewById(R.id.rrValue);
final TextView tv = (TextView)findViewById(R.id.tv);
rrValue.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) {
if(s.length()>0){
tv.setVisibility(View.GONE);
}else
{
tv.setVisibility(View.VISIBLE);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});