gpt4 book ai didi

android - 如何同时监听触摸事件和软键盘事件

转载 作者:太空狗 更新时间:2023-10-29 14:36:13 24 4
gpt4 key购买 nike

我想在 EditText 上同时收听三个不同的事件

  1. 文本更改事件
  2. 触摸事件
  3. 软键盘事件

我正在使用带软键盘的触摸屏。具体来说,我想将这些事件记录到一个文件中以供进一步处理。问题是我无法同时收听所有三个事件。另外,我根本无法捕捉到软键盘事件。当我搜索时,我发现我用于按键事件的代码仅适用于硬键盘,但我想从软键盘捕获这些事件。

我在堆栈溢出中阅读了几个答案,但它们并没有解决我的问题,例如

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

关于android - 如何同时监听触摸事件和软键盘事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com