描述:由于不知道原因,我尽量说全些以便分析:
一:我用了immersionbar控件:https://github.com/gyf-dev/ImmersionBar
(使用了全屏和AlerDialog)
二:我将findViewById和一些简单的设置放在findViewSimple方法中,其中就有
自定义控件: mSeniorEditTextControl
三:APP启动时,需要从数据库中取值,对控件做一些初始化,该过程封装在setDataFromPreferences方法中。
奇怪的事就发生在这里 我明明先调用了findViewSimple,然后调用setDataFromPreferences()初始化data,在setDataFromPreferences中其他控件设置都正常,就是在给mSeniorEditTextControl 设置的时候就会导致不断调用自己(反复调用setDataFromPreferences方法)
下面贴出代码,由于代码比较臃肿,现在功能没写完呢就将近700行了,所以只贴出相关代码。
public class MainActivity extends AppCompatActivity implements SeniorEditTextControl.onValueChangeListener, SeniorEditTextControl.onClickbuttonRightListener, SeniorEditTextControl.onClickbuttonLeftListener, DialogInterface.OnDismissListener, View.OnClickListener, RadioGroup.OnCheckedChangeListener {
//Step1
private MyApplication mApp;
private MySharedPreferences mMySharedPreferences;
private CheckBox mCbEditLock;
private boolean mLock;//CheckBox:cb_lock 的状态 true 为锁头打开状态
private ImageButton mIbtnExit;//退出程序回到桌面按钮
private SeniorEditTextControl mSeniorEditTextControl;// 下边的输入文本框及左右两个按钮
private LinearLayout mLayout_ware;
private TextView mTextView;
private VideoView mLGvideoview;
//dialog
private AlertDialog mAlertDialog;
private Window mDialogWindow;
private int mId = 0;
private RadioGroup mRgRow_textOrientation;
private RadioGroup mRgRow_TextColor;
private RadioGroup mRgRow_bkClor;
private RadioGroup mRgRow_mp4bk;
private View mDialogView;
private ImageButton mBtnOk;
//dialog RadioButton 背景单选按钮 两行不能互斥,顾做如下处理:
private RadioButton mRbtn_bkcolor1;
private RadioButton mRbtn_bkcolor2;
private RadioButton mRbtn_bkcolor3;
private RadioButton mRbtn_bkcolor4;
private RadioButton mRbtn_bkcolor5;
private RadioButton mRbtn_bkcolor6;
private RadioButton mRbtn_bkcolor7;
private RadioButton mRbtn_bkcolor8;
private RadioButton mRbtn_bkcolor9;
private RadioButton mRbtn_bkcolor10;
private RadioButton mRbtn_mp4bk1;
private RadioButton mRbtn_mp4bk2;
private RadioButton mRbtn_mp4bk3;
private RadioButton mRbtn_mp4bk4;
private RadioButton mRbtn_mp4bk5;
private RadioButton mRbtn_mp4bk6;
private RadioButton mRbtn_mp4bk7;
private RadioButton mRbtn_mp4bk8;
private RadioButton mRbtn_mp4bk9;
private RadioButton mRbtn_mp4bk10;
private RadioButton[] mRbtnArray;
private String mResouceID_RadioButton_Tag;
private long mStartEventTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mApp = MyApplication.getInstance();
mMySharedPreferences = new MySharedPreferences(this);
fullScreenKeepOn();
findViewSimple();
setDataFromPreferences(System.currentTimeMillis() - 2000);
// mSeniorEditTextControl.setmMessage(mApp.m.getF_DataMessage());
}
private void fullScreenKeepOn(){
ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_BAR).init();//25 隐藏全部(导航栏和状态栏)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//保持屏幕常亮
}
/**
* 根据持久化数据,初始化各种参数。
*/
private void setDataFromPreferences(long pEventTime){
// Log.d("LKFTAG", "System.currentTimeMillis():"+ System.currentTimeMillis() +
// " pEventTime : " + pEventTime);
if ((System.currentTimeMillis() - pEventTime) < 1000 ){return;}// 800毫秒内无效
Log.d("LKFTAG", "setDataFromPreferences:");
mApp.m = mMySharedPreferences.ReadData();//加载数据
//文本控件横屏显示,竖屏显示
switch (mApp.m.getmTextOrientation()){
case "rbtn_horizontal":
mTextView = createTextViewHorizontal();
break;
case "rbtn_vertical":
mTextView = createTextView();
break;
}
addViewTomLayout_ware(mTextView, Fonts.ARIAL);
//设置文字
String lDataMessage = mApp.m.getF_DataMessage();
mTextView.setText(lDataMessage);
// mSeniorEditTextControl.setmMessage(lDataMessage);//放在这里会不断调用setDataFromPreferences?
//设置字体颜色
mTextView.setTextColor(Color.parseColor(mApp.m.getF_TextColor()));
//设置背景
String lBackground = mApp.m.getF_Background();
if (lBackground != null && lBackground != "") {
if (lBackground.startsWith("#")){
mLGvideoview.stopPlayback();
mLayout_ware.setBackgroundColor(Color.parseColor(lBackground));
}else {
mp4Playmp(lBackground);
}
}
}
//region 页面上基本的按钮(不包括设置页面) start
/**
* findViewById和一些简单的处理
*/
private void findViewSimple(){
initAlerDialog();
mLGvideoview = findViewById(R.id.lg_Videoview);
mLayout_ware = findViewById(R.id.layout_ware);// mLayout_ware.addView(viewInstance) mLayout_ware.removeView(viewInstance)
//输入框及两个功能按钮
mSeniorEditTextControl = findViewById(R.id.sec_SeniorEditTextControl);
// mSeniorEditTextControl.setmMessage( mApp.m.getF_DataMessage()); //写在这里可以
mSeniorEditTextControl.setonValueChangeListener(this);
mSeniorEditTextControl.setonClickbuttonRightListener(this);
mSeniorEditTextControl.setonClickbuttonLeftistener(this);
mSeniorEditTextControl.setEditTextOnFocusChangeListener(new SeniorEditTextControl.EditTextOnFocusChangeListener() {
@Override
public void editTextOnFocusChange(View v, boolean hasFocus) {
//调整操作条的位置
ViewGroup.LayoutParams l_LayoutParams = mSeniorEditTextControl.getLayoutParams();
mSeniorEditTextControl.setGravity(Gravity.CENTER);
mSeniorEditTextControl.setLayoutParams(l_LayoutParams);
}
});
//退出APP返回桌面
mIbtnExit = findViewById(R.id.ibtn_exit);
mIbtnExit.setOnClickListener((v -> {
//回到桌面
Intent home=new Intent(Intent.ACTION_MAIN);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
}));
//是否可编辑:即开锁状态表示触摸任何地方会弹出编辑条:SeniorEditTextControl,锁住状态则不弹出
mCbEditLock = findViewById(R.id.cb_editLock);
mCbEditLock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mLock = isChecked;
DeviceTools.viewVisibleSwitch(mSeniorEditTextControl);
}
});
}