1、一般來說,EditText是不會頂格或者置頂輸入的,文字與EditText的邊框有一小段距離,可用padding實現:
android:paddingLeft="10dp"
android:paddingTop="10dp"
2、如果你的EditText的背景是黑色或者其他顏色的話,有可能會看不到光標,可用下面代碼實現光標顏色與字體顏色一致:
android:textCursorDrawable="@null"
3、當你的EditText的高度較高的時候,比如說200dp,你會發現,你輸入的文字在EditText的中間而不是頂部,設置gravity可達到輸入文字位置的控制:
android:gravity="left|top"
4、EditText內部輸入時隱藏的文字可用hint設置,如果不是純文字,而是文字與圖片結合成的hint,有一種比較簡單的方法,就是用相對佈局包裹EditText,然後在EditText上下一個佈局,就是你需要的hint的佈局,在Activity中,設置EditText得到焦點的時候,hint佈局隱藏,失去焦點的時候,如果EditText裏面沒有文本,則顯示該佈局,如果已經有文本了,則隱藏改佈局:
private void et_hint() {
main_report_et_notes.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// hasFocus 爲true時表示editText控件得到焦點
if (hasFocus) {
main_report_tv_ivandtv.setVisibility(View.GONE);
}
}
});
}
EditText失去焦點的事件,可以設置爲點擊Activity的父佈局的時候,EditText就失去焦點,需要注意的是,如果你的父佈局只有一頁,那你則可以直接設置父佈局的點擊事件即可,如果父佈局裏面嵌套有ScrollView,則設置父佈局的點擊事件,是沒效果的,要設置ScrollView的點擊事件纔可以,然後,失去焦點之後的操作都可以在點擊事件裏面完成,比如說顯示hint之類的,還有,就是默認的EditText點擊EditText之外的地方,輸入法是不會消失的,這時候就有一種做法,就是讓EditText失去焦點的時候,隱藏系統的輸入法,具體操作如下:
main_report_rl_memo_father.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//relativeLayout獲取焦點
main_report_rl_memo_father.setFocusable(true);
main_report_rl_memo_father.setFocusableInTouchMode(true);
main_report_rl_memo_father.requestFocus();
//隱藏輸入法
InputMethodManager imm = (InputMethodManager)
Report.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(main_report_rl_memo_father.getWindowToken(), 0);
//失去焦點的時候,如果EditText爲空,則讓爲hint的佈局顯示
if ("".equals(main_report_et_notes.getText().toString())) {
main_report_tv_ivandtv.setVisibility(View.VISIBLE);
}
return false;
}
});
注:如果對EditText失去焦點之後沒什麼實例化的操作,可以將幾大佈局封裝爲一個類,然後在需要的EditText上直接調用類。
5、點擊EditText輸入的時候,EditText在輸入法上方的實現方法,就是在Manifest上,在對應的Activity中添加這麼一句話:
android:windowSoftInputMode="stateVisible|adjustResize"
6、當你的EditText有一定高度,輸入文本較多的時候,如果用戶想看到一開始輸入的文字,則需要EditText內部可滾動,在xml中加入下面代碼即可實現:
android:scrollbars="vertical"
如果EditText中嵌套了ScrollView,則需要重寫一個,解決滑動衝突事件,重寫ScrollView,並在xml引用重寫的ScrollView即可,重寫代碼如下:
public class VerticalScrollview extends ScrollView {
public VerticalScrollview(Context context) {
super(context);
}
public VerticalScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
return false;
default:
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return true;
}
}