gpt4 book ai didi

Android onKey() 永远不会被调用

转载 作者:行者123 更新时间:2023-11-29 17:51:13 25 4
gpt4 key购买 nike

我想监听我的 EditText 的 的关键事件,每当按下 next 时,我想将 EditText 的文本保存到我的数据中目的。我实现了 OnKeyListener 及其 onKey() 方法,该方法从未被调用过,也不知道为什么……这是我的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
v = inflater.inflate(R.layout.frag_userprofile_myinfo, container, false);
context = getActivity().getApplicationContext();
global = (eKeshGlobal) context;

pbWait = (ProgressBar) v.findViewById(R.id.pbWait);

bnDone = (Button) v.findViewById(R.id.bnDone);
rlBack = (RelativeLayout) v.findViewById(R.id.rlBack);

etFirstName = (EditText) v.findViewById(R.id.etFirstName);
etLastName = (EditText) v.findViewById(R.id.etLastName);
etAdress = (EditText) v.findViewById(R.id.etAdress);
etMobile = (EditText) v.findViewById(R.id.etMobile);
etDob = (EditText) v.findViewById(R.id.etDob);
etOib = (EditText) v.findViewById(R.id.etOib);

etFirstName.setOnKeyListener(this);
etLastName.setOnKeyListener(this);
etAdress.setOnKeyListener(this);
etMobile.setOnKeyListener(this);
etDob.setOnKeyListener(this);
etOib.setOnKeyListener(this);


return v;

}

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode ==
KeyEvent.KEYCODE_ENTER)) {

switch(v.getId()) {

case R.id.etFirstName:
user.setFirstName(etFirstName.getText().toString());
return true;

case R.id.etLastName:
user.setLastName(etLastName.getText().toString());
return true;

case R.id.etAdress:
user.setAddress(etAdress.getText().toString());
return true;

case R.id.etMobile:
user.setMobile(etMobile.getText().toString());
return true;

case R.id.etDob:
user.setDateOfBirth(Utils.formatDate(context,
etDob.getText().toString()));
return true;

case R.id.etOib:
user.setOib(etOib.getText().toString());
return true;

default:
return false;
}
}

return false;
}

当然我已经实现了监听器:

public class UserProfileMyInfo extends Fragment implements View.OnKeyListener

下面是 EditText xml 的示例:

<EditText
android:id="@+id/etLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ems="10"
android:hint="@string/my_profile_lastname"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true" />

我真的没有办法完成这项工作,我也尝试过 onKeyDown() 但也没有用。提前致谢!

最佳答案

来自文档,View.OnKeyListener用于硬件键。这就是它不起作用的原因。

Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

关于Android onKey() 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22690865/

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