寫代碼的四點:
1.明确需求。要做什麼?
2.分析思路。要怎麼做?(1,2,3……)
3.确定步驟。每一個思路要用到哪些語句、方法和對象。
4.代碼實作。用具體的語言代碼将思路實作出來。
學習新技術的四點:
1.該技術是什麼?
2.該技術有什麼特點?(使用需注意的方面)
3.該技術怎麼使用?(寫Demo)
4.該技術什麼時候用?(在Project中的使用場景 )
----------------------早計劃,早準備,早完成。-------------------------
代碼如下:
package com.wy.test.other;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.reflect.Field;
/**
* 禁止複制粘貼功能的EditText(用于密碼)
* 1.禁止長按;
* 2.禁止文本選中;
* 3.處理橫屏;
* 4.使用者選擇操作無效化處理
* 5.處理小米/OPPO手機(反射 android.widget.Editor 修改彈框菜單不顯示);
*/
public class NullMenuEditText extends EditText {
public NullMenuEditText(Context context, AttributeSet attrs) {
super(context, attrs);
//禁止長按
setLongClickable(false);
//禁止文本選中
setTextIsSelectable(false);
//EditText在橫屏的時候會出現一個新的編輯界面,是以要禁止掉這個新的編輯界面;
//新的編輯界面裡有複制粘貼等功能按鈕,目前測試是無效果的,以防外一,建議禁止掉。
setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
//使用者選擇操作無效化處理
setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// setInsertionDisabled when user touches the view
setInsertionDisabled();
}
return false;
}
});
}
/**
* 小米/OPPO手機上禁止複制粘貼功能
* 反射 android.widget.Editor 修改彈框菜單不顯示
*/
private void setInsertionDisabled() {
try {
Field editorField = TextView.class.getDeclaredField("mEditor");
editorField.setAccessible(true);
Object editorObject = editorField.get(this);
Class editorClass = Class.forName("android.widget.Editor");
Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled");
mInsertionControllerEnabledField.setAccessible(true);
mInsertionControllerEnabledField.set(editorObject, false);
Field mSelectionControllerEnabledField = editorClass.getDeclaredField("mSelectionControllerEnabled");
mSelectionControllerEnabledField.setAccessible(true);
mSelectionControllerEnabledField.set(editorObject, false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onTextContextMenuItem(int id) {
return true;
}
@Override
public boolean isSuggestionsEnabled() {
return false;
}
}
在項目中的應用:
在xml布局檔案中直接使用;
<com.wy.test.other.NullMenuEditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#eeeeee" />
---------------------------------------------------------------------------------------------------------------------------
早計劃,早準備,早完成。 歡迎關注!交流!Star!
GitHub:https://github.com/wangyang0313
微信公衆号:一個靈活的胖子MrWang
簡書:https://www.jianshu.com/u/e5e733d79b96