android keyboardview去掉点击效果,KeyboardView is deprecated in android

问题

KeyboardView has been deprecated official by android team in API level 29 and i didn't able to find the alternative way for this. Please let me know if there any alternative?

回答1:

From the docs here :

This class was deprecated in API level 29. This class is deprecated

because this is just a convenient UI widget class that application

developers can re-implement on top of existing public APIs. If you

have already depended on this class, consider copying the

implementation from AOSP into your project or re-implementing a

similar widget by yourselves

This means that you have to create your own view with all the keys, which also means handling all the click events like enter, delete and switching keyboards eg. to symbols etc. by yourself.

Actually there are many ways to do it. But I will try to give you a simple idea, you will follow most of the steps that you used while using the deprecated KeyboardView:

First create your custom keyboard layout with android:layout_height="wrap_content", you can use any layout depending what is convenient for you like LinearLayout, RelativeLayout and Buttons for the keys. I used a GridLayout with Buttons.

Then create the subclass of InputMethodService as usual:

public class MyIMService extends InputMethodService implements View.OnClickListener {

private static String TAG = "MyIMService";

@Override

public View onCreateInputView() {

View myKeyboardView = getLayoutInflater().inflate(R.layout.key_layout, null);

Button btnA = myKeyboardView.findViewById(R.id.btnA);

btnA.setOnClickListener(this);

//ADD ALL THE OTHER LISTENERS HERE FOR ALL THE KEYS

return myKeyboardView;

}

@Override

public void onClick(View v) {

//handle all the keyboard key clicks here

InputConnection ic = getCurrentInputConnection();

if (v instanceof Button) {

String clickedKeyText = ((Button) v).getText().toString();

ic.commitText(clickedKeyText, 1);

}

}

}

The key_layout should contains all the keys can be any layout, I used a GridLayout which resembles a typical keyboard. For switching between symbols or numbers you should update the text of the buttons etc. As I said earlier you can try a different way of handling all the click events. But this should give you the basic idea.

That's it. You have to add this service in your manifest file as usual and also the other steps as usual. This should work now.

来源:https://stackoverflow.com/questions/60316785/keyboardview-is-deprecated-in-android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值