为什么inAppKeyboard不显示在屏幕上?

时间:2020-11-06 09:46:19

标签: java android

我正在尝试制作一个应用内键盘(只有0-9个按钮) 我的main_activity.xml中有一个Edittext和一个键盘 但是当我打开应用程序时,键盘没有显示。 当我单击编辑文本时,将显示主移动键盘。 我希望应用程序内的键盘始终显示在屏幕上(即使焦点不在editText上也是如此)

这是我的代码:

main_activity.xml:

<EditText
    android:id="@+id/edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="enter text"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.example.inappkeyboard.MyKeyboard
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"/>

keyboard.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"/>
    <Button
        android:id="@+id/button_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2"/>
    <Button
        android:id="@+id/button_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3"/>
    <Button
        android:id="@+id/button_4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="4"/>
    <Button
        android:id="@+id/button_5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="5"/>
</LinearLayout>

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button_6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="6"/>
    <Button
        android:id="@+id/button_7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="7"/>
    <Button
        android:id="@+id/button_8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="8"/>
    <Button
        android:id="@+id/button_9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="9"/>
    <Button
        android:id="@+id/button_0"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0"/>
</LinearLayout>

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button_delete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="delete"/>
    <Button
        android:id="@+id/button_enter"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="enter"/>

</LinearLayout>

MyKeyboard类:

public class MyKeyboard extends LinearLayout implements View.OnClickListener {

private Button button1, button2 ,button3, button4,button5, button6,button7,button8,button9,button0,buttonDel, buttonEn;

private SparseArray<String> keyValues = new SparseArray<>();
private InputConnection inputConnection;

public MyKeyboard(Context context) {
    super(context, null, 0);
}

public MyKeyboard(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public MyKeyboard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context,attrs);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyKeyboard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

private void init(Context context, AttributeSet attrs){
    LayoutInflater.from(context).inflate(R.layout.keyboard,this,true);
    button0 = findViewById(R.id.button_0);
    button1 = findViewById(R.id.button_1);
    button2 = findViewById(R.id.button_2);
    button3 = findViewById(R.id.button_3);
    button4 = findViewById(R.id.button_4);
    button5 = findViewById(R.id.button_5);
    button6 = findViewById(R.id.button_6);
    button7 = findViewById(R.id.button_7);
    button8 = findViewById(R.id.button_8);
    button9 = findViewById(R.id.button_9);
    buttonDel = findViewById(R.id.button_delete);
    buttonEn = findViewById(R.id.button_enter);

    button0.setOnClickListener(this);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    button4.setOnClickListener(this);
    button5.setOnClickListener(this);
    button6.setOnClickListener(this);
    button7.setOnClickListener(this);
    button8.setOnClickListener(this);
    button9.setOnClickListener(this);
    buttonDel.setOnClickListener(this);
    buttonEn.setOnClickListener(this);

    keyValues.put(R.id.button_0,"0");
    keyValues.put(R.id.button_1,"1");
    keyValues.put(R.id.button_2,"2");
    keyValues.put(R.id.button_3,"3");
    keyValues.put(R.id.button_4,"4");
    keyValues.put(R.id.button_5,"5");
    keyValues.put(R.id.button_6,"6");
    keyValues.put(R.id.button_7,"7");
    keyValues.put(R.id.button_8,"8");
    keyValues.put(R.id.button_9,"9");
    keyValues.put(R.id.button_enter,"\n");


}

@Override
public void onClick(View view) {

    if (inputConnection == null)
        return;
    if (view.getId() == R.id.button_delete){
        CharSequence selectedText = inputConnection.getSelectedText(0);
        if (TextUtils.isEmpty(selectedText)){
            inputConnection.deleteSurroundingText(1,0);
        }else {
            inputConnection.commitText("",1);
        }
    }else {
        String value = keyValues.get(view.getId());
        inputConnection.commitText(value,1);
    }
}
public void setInputConnection(InputConnection ic){
    inputConnection = ic;
}

}

最后是mainActivity:

public class MainActivity extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText editText = findViewById(R.id.edit_text);
    MyKeyboard keyboard = findViewById(R.id.keyboard);

    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setTextIsSelectable(true);

    InputConnection ic = editText.onCreateInputConnection(new EditorInfo());
    keyboard.setInputConnection(ic);
    editText.setKeyboardNavigationCluster(false);
}

}

0 个答案:

没有答案