Merge pull request #532 from bobyang3/RY-bopomofo-keyboard

Ry bopomofo keyboard
pull/586/head
FongMi 1 year ago
parent 251edab835
commit 661a7f90c4
  1. 56
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/SearchActivity.java
  2. 50
      app/src/leanback/java/com/fongmi/android/tv/ui/adapter/KeyboardAdapter.java
  3. 16
      app/src/leanback/java/com/fongmi/android/tv/ui/custom/CustomKeyboard.java
  4. 10
      app/src/leanback/res/drawable/ic_action_zhuyin.xml

@ -34,6 +34,7 @@ import com.google.common.net.HttpHeaders;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import okhttp3.Call;
@ -50,6 +51,59 @@ public class SearchActivity extends BaseActivity implements WordAdapter.OnClickL
activity.startActivity(new Intent(activity, SearchActivity.class));
}
public static String convertBopomofoToPinyin(String bopomofoString) {
HashMap<String, String> bopomofoToPinyinMap = new HashMap<String, String>();
bopomofoToPinyinMap.put("ㄅ", "b");
bopomofoToPinyinMap.put("ㄆ", "p");
bopomofoToPinyinMap.put("ㄇ", "m");
bopomofoToPinyinMap.put("ㄈ", "f");
bopomofoToPinyinMap.put("ㄉ", "d");
bopomofoToPinyinMap.put("ㄊ", "t");
bopomofoToPinyinMap.put("ㄋ", "n");
bopomofoToPinyinMap.put("ㄌ", "l");
bopomofoToPinyinMap.put("ㄍ", "g");
bopomofoToPinyinMap.put("ㄎ", "k");
bopomofoToPinyinMap.put("ㄏ", "h");
bopomofoToPinyinMap.put("ㄐ", "j");
bopomofoToPinyinMap.put("ㄑ", "q");
bopomofoToPinyinMap.put("ㄒ", "x");
bopomofoToPinyinMap.put("ㄓ", "z");
bopomofoToPinyinMap.put("ㄔ", "c");
bopomofoToPinyinMap.put("ㄕ", "s");
bopomofoToPinyinMap.put("ㄖ", "r");
bopomofoToPinyinMap.put("ㄗ", "z");
bopomofoToPinyinMap.put("ㄘ", "c");
bopomofoToPinyinMap.put("ㄙ", "s");
bopomofoToPinyinMap.put("ㄚ", "a");
bopomofoToPinyinMap.put("ㄛ", "o");
bopomofoToPinyinMap.put("ㄜ", "e");
bopomofoToPinyinMap.put("ㄝ", "e"); // can also be ie
bopomofoToPinyinMap.put("ㄞ", "ai");
bopomofoToPinyinMap.put("ㄟ", "ei");
bopomofoToPinyinMap.put("ㄠ", "ao");
bopomofoToPinyinMap.put("ㄡ", "ou");
bopomofoToPinyinMap.put("ㄢ", "an");
bopomofoToPinyinMap.put("ㄣ", "en");
bopomofoToPinyinMap.put("ㄤ", "ang");
bopomofoToPinyinMap.put("ㄥ", "eng");
bopomofoToPinyinMap.put("ㄦ", "er");
bopomofoToPinyinMap.put("ㄧ", "y");
bopomofoToPinyinMap.put("ㄨ", "w");
bopomofoToPinyinMap.put("ㄩ", "yu");
StringBuilder pinyinStringBuilder = new StringBuilder();
for (char bopomofoChar : bopomofoString.toCharArray()) {
String pinyin = bopomofoToPinyinMap.get(String.valueOf(bopomofoChar));
if (pinyin != null) {
pinyinStringBuilder.append(pinyin);
} else {
// Handle characters not found in the map (e.g., tones)
pinyinStringBuilder.append(bopomofoChar); // Add the character as is
}
}
return pinyinStringBuilder.toString();
}
@Override
protected ViewBinding getBinding() {
return mBinding = ActivitySearchBinding.inflate(getLayoutInflater());
@ -141,7 +195,7 @@ public class SearchActivity extends BaseActivity implements WordAdapter.OnClickL
mBinding.keyword.setSelection(mBinding.keyword.length());
Util.hideKeyboard(mBinding.keyword);
if (TextUtils.isEmpty(keyword)) return;
CollectActivity.start(this, keyword);
CollectActivity.start(this, convertBopomofoToPinyin(keyword));
App.post(() -> mRecordAdapter.add(keyword), 250);
}

@ -1,5 +1,7 @@
package com.fongmi.android.tv.ui.adapter;
import static java.util.Locale.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -8,22 +10,53 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
import com.fongmi.android.tv.databinding.AdapterKeyboardIconBinding;
import com.fongmi.android.tv.databinding.AdapterKeyboardTextBinding;
import java.util.Arrays;
import java.util.List;
public class KeyboardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final OnClickListener mListener;
private final List<Object> mItems;
private List<Object> mItems;
private List<Object> englishKeys =Arrays.asList(
R.drawable.ic_keyboard_remote
, R.drawable.ic_keyboard_left
, R.drawable.ic_keyboard_right
, R.drawable.ic_keyboard_back
, R.drawable.ic_keyboard_search
, R.drawable.ic_setting_home
, R.drawable.ic_action_zhuyin
, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
private List<Object> zhuYinKeys =Arrays.asList(
R.drawable.ic_keyboard_remote
, R.drawable.ic_keyboard_left
, R.drawable.ic_keyboard_right
, R.drawable.ic_keyboard_back
, R.drawable.ic_keyboard_search
, R.drawable.ic_setting_home
, R.drawable.ic_action_zhuyin
,"ㄅ", "ㄆ", "ㄇ", "ㄈ", "ㄉ", "ㄊ", "ㄋ", "ㄌ", "ㄍ", "ㄎ", "ㄏ", "ㄐ", "ㄑ", "ㄒ",
"ㄓ", "ㄔ", "ㄕ", "ㄖ", "ㄗ", "ㄘ", "ㄙ", "ㄚ", "ㄛ", "ㄜ", "ㄝ", "ㄞ", "ㄟ", "ㄠ", "ㄡ", "ㄢ", "ㄣ",
"ㄤ", "ㄥ", "ㄦ", "ㄧ", "ㄨ", "ㄩ"
, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
public KeyboardAdapter(OnClickListener listener) {
this.mItems = Arrays.asList(R.drawable.ic_keyboard_remote, R.drawable.ic_keyboard_left, R.drawable.ic_keyboard_right, R.drawable.ic_keyboard_back, R.drawable.ic_keyboard_search, R.drawable.ic_setting_home, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if(Setting.getLanguage()==2){
this.mItems = zhuYinKeys;
}else{
this.mItems = englishKeys;
}
this.mListener = listener;
}
public interface OnClickListener {
void onTextClick(String text);
@ -33,6 +66,19 @@ public class KeyboardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
boolean onLongClick(int resId);
}
public void updateKeyList(List<Object> newKeys) {
this.mItems = newKeys;
notifyDataSetChanged();
}
public List<Object> getEnglishKeys() {
return englishKeys;
}
public List<Object> getZhuYinKeys() {
return zhuYinKeys;
}
@Override
public int getItemViewType(int position) {
return mItems.get(position) instanceof String ? 0 : 1;

@ -1,15 +1,18 @@
package com.fongmi.android.tv.ui.custom;
import android.annotation.SuppressLint;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
import com.fongmi.android.tv.databinding.ActivitySearchBinding;
import com.fongmi.android.tv.ui.adapter.KeyboardAdapter;
import java.util.List;
public class CustomKeyboard implements KeyboardAdapter.OnClickListener {
private final ActivitySearchBinding binding;
private final Callback callback;
private boolean useZhuYin = false;
private KeyboardAdapter keyboardAdapter;
public static void init(Callback callback, ActivitySearchBinding binding) {
new CustomKeyboard(callback, binding).initView();
@ -18,12 +21,16 @@ public class CustomKeyboard implements KeyboardAdapter.OnClickListener {
public CustomKeyboard(Callback callback, ActivitySearchBinding binding) {
this.callback = callback;
this.binding = binding;
this.keyboardAdapter = new KeyboardAdapter(this);
}
private void initView() {
binding.keyboard.setHasFixedSize(true);
binding.keyboard.addItemDecoration(new SpaceItemDecoration(6, 8));
binding.keyboard.setAdapter(new KeyboardAdapter(this));
if(Setting.getLanguage()==2){
useZhuYin=true;
}
binding.keyboard.setAdapter(keyboardAdapter);
}
@Override
@ -42,6 +49,11 @@ public class CustomKeyboard implements KeyboardAdapter.OnClickListener {
StringBuilder sb = new StringBuilder(binding.keyword.getText().toString());
int cursor = binding.keyword.getSelectionStart();
switch (resId) {
case R.drawable.ic_action_zhuyin:
useZhuYin = !useZhuYin;
List<Object> newKeys = useZhuYin ? keyboardAdapter.getZhuYinKeys() : keyboardAdapter.getEnglishKeys();
keyboardAdapter.updateKeyList(newKeys);
break;
case R.drawable.ic_setting_home:
callback.showDialog();
break;

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp"
android:height="20dp"
android:tint="#FFFFFF"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M20,5L4,5c-1.1,0 -1.99,0.9 -1.99,2L2,17c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,7c0,-1.1 -0.9,-2 -2,-2zM11,8h2v2h-2L11,8zM11,11h2v2h-2v-2zM8,8h2v2L8,10L8,8zM8,11h2v2L8,13v-2zM7,13L5,13v-2h2v2zM7,10L5,10L5,8h2v2zM16,17L8,17v-2h8v2zM16,13h-2v-2h2v2zM16,10h-2L14,8h2v2zM19,13h-2v-2h2v2zM19,10h-2L17,8h2v2z"/>
</vector>
Loading…
Cancel
Save