我想在EditText上同时听三个不同的事件
我正在使用带软键盘的触摸屏。具体来说,我想将这些事件记录到文件中以供进一步处理。问题是我无法同时收听所有三个事件。此外,我根本无法捕捉到软键盘事件。当我搜索时,我发现我用于键事件的代码仅适用于硬键盘,但我想从软键盘中捕获这些事件。
我在堆栈溢出中读了几个答案但是它们没有解决我的问题,比如
How to combine OnClickListener and OnTouchListener for an ImageButton
touchEvent while typing with soft keyboard android
Receiving onTouch and onClick events with Android
我一直试图解决这个问题好几天但是我没有成功。我在触摸监听器上返回false以允许其他监听器捕获事件但是这也没有解决问题。任何帮助或指导将受到高度赞赏。
phrase.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String formattedDate = df.format(c.getTime());
String[] data = {formattedDate,"Acceleration","","","","","","","","","","","","","", "",
"","","", "", "", "", "","", "", "", "", "", "", "", "","", "", "","", "", "", "", "", "", "", "",
"During",timeStamp+"",Xacc+"",Yacc+"",Zacc+"",};
writeDataToXls(data);
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
String formattedDate = df.format(c.getTime());
String[] data = {formattedDate,"Acceleration","","","","","","","","","","","","","", "",
"","","", "", "", "", "","", "", "", "", "", "", "", "","", "", "","", "", "", "", "", "", "", "",
"During",timeStamp+"",Xacc+"",Yacc+"",Zacc+"",};
writeDataToXls(data);
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
String formattedDate = df.format(c.getTime());
String[] data = {formattedDate,"Acceleration","","","","","","","","","","","","","", "",
"","","", "", "", "", "","", "", "", "", "", "", "", "","", "", "","", "", "", "", "", "", "", "",
"During",timeStamp+"",Xacc+"",Yacc+"",Zacc+"",};
writeDataToXls(data);
}
});
phrase.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String formattedDate = df.format(c.getTime());
int action=event.getAction();
int actionIndex=event.getActionIndex();
int actionMasked= event.getActionMasked();
int btnState=event.getButtonState();
long downTime=event.getDownTime();
long eventTime=event.getEventTime();
int EdgeFlags=event.getEdgeFlags();
int Flags= event.getFlags();
int MetaState=event.getMetaState();
float orientation=event.getOrientation();
int pointerCount=event.getPointerCount();
float pressure=event.getPressure();
float size=event.getSize();
int source=event.getSource();
float toolMajor=event.getToolMajor();
float toolMinor=event.getToolMinor();
float touchMajor=event.getTouchMajor();
float touchMinor=event.getTouchMinor();
float xAxis=event.getAxisValue(AXIS_X);
float yAxis=event.getAxisValue(AXIS_Y);
float x=event.getX();
float y=event.getY();
float rawX=event.getRawX();
float rawY=event.getRawY();
float XPrecision=event.getXPrecision();
float YPrecision=event.getYPrecision();
String[] data = {formattedDate,"Touch","","","","","","","","","","","","", "","",
action+"",actionIndex+"",actionMasked+"", btnState+"", downTime+"", eventTime+"",EdgeFlags+"",Flags+"", MetaState+"",
orientation+"",pointerCount+"", pressure+"", size+"", source+"", toolMajor+"",toolMinor+"", touchMajor+"", touchMinor+"",
xAxis+"", yAxis+"", x+"", y+"", rawX+"", rawY+"", XPrecision+"", YPrecision+""
,timeStamp+"",Xacc+"",Yacc+"",Zacc+"",};
writeDataToXls(data);
System.out.println("touch");
return false;
}
});
phrase.setOnKeyListener(new EditText.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
String formattedDate = df.format(c.getTime());
int action= event.getAction();
int keyCode2=event.getKeyCode();
char dispLabel=event.getDisplayLabel();
long downtime=event.getDownTime();//in ms
long eventTime= event.getEventTime();//in ms
int flags=event.getFlags();
int metaState=event.getMetaState();
int modifiers=event.getModifiers();
int number= event.getNumber();
int repeatCount= event.getRepeatCount();
int scanCode= event.getScanCode();
int source= event.getSource();
int unicodeChar=event.getUnicodeChar();
String[] data = {formattedDate,"Key",action+"",keyCode+"",keyCode2+"",dispLabel+"",downtime+"",eventTime+"",flags+"",metaState+"",modifiers+"",number+"",repeatCount+"",scanCode+"", source+"",unicodeChar+"",
"","","", "", "", "", "","", "", "", "", "", "", "", "","", "", "","", "", "", "", "", "", "", "",
"","","","","","","",""};
writeDataToXls(data);
System.out.println("Key");
return true;
}
});
我必须创建自定义键盘,以便在同时获取加速度数据的同时处理触摸和按键。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_typing, container, false);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
senSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
initializeTheCSVfile();
mKeyboard = new Keyboard(getActivity(), R.xml.keyboard);
phrase= view.findViewById(R.id.typing_edittxt);
okBtn=view.findViewById(R.id.okbtn);
okBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String formattedDate = df.format(c.getTime());
int ld=Utilities.getLevenshteinDistance("earth quakes are predictable",phrase.getText().toString());
String[] data = {formattedDate,"LevenshteinDistance=",ld+""};
writeDataToXls(data);
mListener.onFragmentInteraction(2,xlsfile);
}
});
phrase.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event); // handle the event first
//Hide the system keyboard when the user touch the edittext
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // hide the soft keyboard
}
showKeyboardWithAnimation();
return true;
}
});
mKeyboardView = (CustomKeyboardView) view.findViewById(R.id.keyboard_view);
mKeyboardView.setKeyboard(mKeyboard);
mKeyboardView
.setOnKeyboardActionListener(new BasicOnKeyboardActionListener(
getActivity())
{
public void onKey(int primaryCode, int[] keyCodes) {
super.onKey( primaryCode,keyCodes);
//aquire the pressed key code and displayed label
KeyPrimaryCode=primaryCode;
typedDisplayedLabel=Character.toLowerCase(displayedLabel);
}
});
mKeyboardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String formattedDate = df.format(c.getTime());
int action=event.getAction();
long downTime=event.getDownTime();
long eventTime=event.getEventTime();
float orientation=event.getOrientation();
int pointerCount=event.getPointerCount();
float pressure=event.getPressure();
float size=event.getSize();
float toolMajor=event.getToolMajor();
float toolMinor=event.getToolMinor();
float touchMajor=event.getTouchMajor();
float touchMinor=event.getTouchMinor();
float xAxis=event.getAxisValue(AXIS_X);
float yAxis=event.getAxisValue(AXIS_Y);
float x=event.getX();
float y=event.getY();
float rawX=event.getRawX();
float rawY=event.getRawY();
float XPrecision=event.getXPrecision();
float YPrecision=event.getYPrecision();
System.out.println("**************TouchKeyboard:"+action);
v.onTouchEvent(event); // handle the event first
if(action==0 && KeyPrimaryCode!=67 && KeyPrimaryCode!=62 ) {
KeyPrimaryCode = -9000;
typedDisplayedLabel='$';
}
else if(action==2 && KeyPrimaryCode!=67 && KeyPrimaryCode!=62 ){
KeyPrimaryCode=-2000;
typedDisplayedLabel='$';}
String[] data = {formattedDate,KeyPrimaryCode+"",typedDisplayedLabel+"",
action+"",downTime+"", eventTime+""
,orientation+"",pointerCount+"", pressure+"", size+"", toolMajor+"",toolMinor+"", touchMajor+"", touchMinor+"",
xAxis+"", yAxis+"", x+"", y+"", rawX+"", rawY+"", XPrecision+"", YPrecision+""
,timeStamp+"",Xacc+"",Yacc+"",Zacc+""};
writeDataToXls(data);
System.out.println("KeyCode:"+KeyPrimaryCode);
System.out.println("Displayed label:"+typedDisplayedLabel);
System.out.println("Handle TouchKeyboard:"+action);
return true;
}
});
return view;
}//on create view