Android 开发实现EditText 光标居右显示
Android开发实现EditText光标居右显示
前言:
有些时候肯定会遇到这种奇葩的需求,光标要靠右显示,因为Android里面光标默认是靠左显示的,那怎么实现呢,肯定有办法的,这里提供一种实现方式,看布局
<FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="6dp" android:background="@null" android:gravity="right|center_vertical" android:text="请输入您想输入的"/> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="right|center_vertical"/> </FrameLayout>
看布局你就明白是什么意思了吧,代码里面监听EditText输入,让TextView显示隐藏就行了。
et.addTextChangedListener(newTextWatcher(){
@Override
publicvoidbeforeTextChanged(CharSequencecharSequence,inti,inti1,inti2){
}
@Override
publicvoidonTextChanged(CharSequencecharSequence,inti,inti1,inti2){
if(TextUtils.isEmpty(charSequence)){
tv.setVisibility(View.VISIBLE);
}else{
tv.setVisibility(View.GONE);
}
}
@Override
publicvoidafterTextChanged(Editableeditable){
}
});
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!