Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中单击edittext时如何显示自定义键盘_Android_Android Edittext_Android Softkeyboard - Fatal编程技术网

在android中单击edittext时如何显示自定义键盘

在android中单击edittext时如何显示自定义键盘,android,android-edittext,android-softkeyboard,Android,Android Edittext,Android Softkeyboard,我的应用程序中有一个自定义键盘。问题是当点击编辑文本时如何弹奏键盘。我使用setonfocuschangre监听器,现在,当edittext焦点改变时,custon键盘会出现。但我想在每次单击edittext时都显示此键盘。我忘记将edittext放在此处的一个信息位于片段中。使用onClickListener如下所示: edit_text.setOnClickListener(new OnClickListener(){ @Override public void onCli

我的应用程序中有一个自定义键盘。问题是当点击编辑文本时如何弹奏键盘。我使用setonfocuschangre监听器,现在,当edittext焦点改变时,custon键盘会出现。但我想在每次单击edittext时都显示此键盘。我忘记将edittext放在此处的一个信息位于片段中。

使用
onClickListener
如下所示:

edit_text.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        custom_keyboard.open();
    }
});
或者您可以这样做:

edit_text.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                custom_keyboard.open();
            else
                custom_keyboard.close();
        }
    });

使用
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u HIDDEN)
要禁用默认键盘,然后设置单击侦听器以显示您自己的键盘,可以尝试以下操作

    edittext.setOnClickListener(new OnClickListener() {
                    // NOTE By setting the on click listener, we can show the custom keyboard again,
                   // by tapping on an edit box that already had focus (but that had the keyboard hidden).
                    @Override public void onClick(View v) {
                        showCustomKeyboard(v);
                    }
          });


          // Disable standard keyboard hard way
          // NOTE There is also an easy way: 'edittext.setInputType(InputType.TYPE_NULL)' 
         // (but you will not have a cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
                edittext.setOnTouchListener(new OnTouchListener() {
                    @Override public boolean onTouch(View v, MotionEvent event) {
                        EditText edittext = (EditText) v;
                        int inType = edittext.getInputType();       // Backup the input type
                        edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
                        edittext.onTouchEvent(event);               // Call native handler
                        edittext.setInputType(inType);              // Restore input type
                        return true; // Consume touch event
                    }
                });


        // Disable spell check (hex strings look like words to Android)
        edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

有关更多信息,请检查

我使用键盘标记在应用程序中创建了自定义键盘。我在屏幕上的一个相对窗口中添加了这个键盘

private void createCustomKeyboard() {
  Keyboard customKeyboard = new Keyboard(getActivity(), R.layout.keyboard);
  CustomKeyboard mCustomKeyboard = new CustomKeyboard(getActivity(), this);
  mCustomKeyboard.setKeyboard(customKeyboard);
  RelativeLayout relLayKeyboard.addView(mCustomKeyboard);  
} 
如果要在一个或多个EditText上使用此自定义键盘,则必须使用以下代码:

EditText edtxtName = (EditText) v.findViewById(R.id.edtName);
RelativeLayout relLayKeyboard = (RelativeLayout)findViewById(R.id.relLay_keyboard);
edtxtName.setOnTouchListener(exitSoftKeyBoard);

private final OnTouchListener exitSoftKeyBoard = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext().getSystemService(
            android.content.Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    if(v.equals(edtxtName)){
        edtxtName.requestFocus();
        relLayKeyboard.setVisibility(View.VISIBLE);
    } 
    return true;
  }
};

实际上,您可以为自定义键盘创建输入连接,然后将此连接设置为编辑文本

使用InputConnection,您的自定义键盘将取代android键盘并作为默认键盘工作。

您可以从这里获取代码:

keyboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <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="2"
            android:text="Delete"/>

        <Button
            android:id="@+id/button_enter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Enter"/>

    </LinearLayout>

</merge>
public class MyKeyboard extends LinearLayout implements View.OnClickListener {

private Button button1, button2, button3, button4,
                button5, button6, button7, button8,
                button9, button0, buttonDelete, buttonEnter;

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

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

public MyKeyboard(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

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

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

    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_0, "0");
    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;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ssaurel.inappkeyboard.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#c9c9f1"
        android:layout_margin="50dp"
        android:padding="5dp"
        android:layout_alignParentTop="true"
        />

    <com.ssaurel.inappkeyboard.MyKeyboard
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = (EditText) findViewById(R.id.editText);
        MyKeyboard keyboard = (MyKeyboard) findViewById(R.id.keyboard);
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);

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

在使用任何用户操作执行自定义键盘视图时,启动编辑文本的输入连接,并创建删除和输入文本的方法

在活动
onCreate
方法上声明
inputConnection
,如下所示

InputConnection inputConnection = editText.onCreateInputConnection(new EditorInfo());
当您输入任何文本时,请执行以下代码

inputConnection.commitText(text, 1);
if (TextUtils.isEmpty(inputConnection.getSelectedText(0))) {
    inputConnection.deleteSurroundingText(1, 0);
} else {
    inputConnection.commitText("", 1);
}
单击清除/退格按钮时,执行以下代码

inputConnection.commitText(text, 1);
if (TextUtils.isEmpty(inputConnection.getSelectedText(0))) {
    inputConnection.deleteSurroundingText(1, 0);
} else {
    inputConnection.commitText("", 1);
}

我将尝试这一点,以确保我忘记在这里放置的一个信息edittext在片段中..我使用onclicklistener来显示我的PopupleBoard,并在侦听器中隐藏键盘。谢谢你的帮助。。。。