在我的项目中,键盘应该在我的Activity

时间:2015-11-25 08:10:25

标签: android android-softkeyboard custom-keyboard

在我的项目中,键盘应该固定在我的Activity中,而不会隐藏,但在我的代码中键盘会在启动活动时自动显示,但问题是单击后退键,键盘是不可见的。

所以任何人都可以建议在我的活动中修复键盘的答案

提前谢谢

enter image description here

在此图片中看到我的键盘想要在此活动中修复。例如计算机键盘想要在编辑文本下修复

2 个答案:

答案 0 :(得分:2)

我的问题得到了解决方案。没有使用软键盘我在我的活动中开发了固定的自定义键盘

public class keyboardActivity1 extends Activity {

    private EditText et;
    Keyboard keyboard;
    KeyboardView keyview;
    boolean caps = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keyboard1);
        //create Keyboard object
        keyboard = new Keyboard(this, R.xml.keypad);
        //create KeyboardView object
        keyview = (KeyboardView) findViewById(R.id.customkeyboard);
        //attache the keyboard object to the KeyboardView object
        keyview.setKeyboard(keyboard);
        //show the keyboard
        keyview.setVisibility(KeyboardView.VISIBLE);
        //take the keyboard to the front
        keyview.bringToFront();
        //register the keyboard to receive the key pressed
        keyview.setOnKeyboardActionListener(new KeyList());
        et = (EditText) findViewById(R.id.txt_edit);

    }

    class KeyList implements KeyboardView.OnKeyboardActionListener {
        public void onKey(View v, int keyCode, KeyEvent event) {

        }
        public void onText(CharSequence text) {

        }

        public void swipeLeft() {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {

        }

        public void swipeUp() {

        }

        public void swipeDown() {

        }

        public void swipeRight() {

        }

        // @TargetApi(Build.VERSION_CODES.KITKAT)
        public void onPress(int primaryCode) {
            // InputConnection ic = getCurrentInputConnection();


            if (primaryCode == -5) { //take the last character out when delete button is pressed.
                String text = et.getText().toString();
                if (et.length() > 0) {
                    text = text.substring(0, text.length() - 1);
                    et.setText(text);
                    et.setSelection(text.length());
                }
            } else if (primaryCode == -1) {

                caps = !caps;
                keyboard.setShifted(caps);
                keyview.invalidateAllKeys();
               // char code = (char) primaryCode;
                //  if (Character.isLetter(code) && caps) {
               // code = Character.toUpperCase(code);
              //  et.append("" + code);


            } else if (primaryCode == 10) {
                String text = et.getText().toString();
                et.setText(text);

            } else {
                char code = (char) primaryCode;

                // else if(Character.isLetter(code) && caps)){


                if (Character.isLetter(code) && caps) {
                    // char code = (char) primaryCode;

                    code = Character.toUpperCase(code);
                    //et.setText(code);
                    et.append(String.ValueOf(code));
                } else { // char code = (char) primaryCode;
                    code = Character.toLowerCase(code);
                    et.append(String.ValueOf(code));
                }
            }
        }

        @Override
        public void onRelease(int i) {

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_keyboard_activity1, menu);
        return true;
    }

}

keyboard activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/txt_edit"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="top"
        />

    <android.inputmethodservice.KeyboardView
        android:visibility="gone"
        android:id="@+id/customkeyboard"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="bottom"
        android:keyPreviewLayout="@layout/preview"

        />

在res / xml / keypad.xml下创建键盘代码

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="40%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="50dp"
    >
    <Row>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="w"/>
        <Key android:codes="101" android:keyLabel="e"/>
        <Key android:codes="114" android:keyLabel="r"/>
        <Key android:codes="116" android:keyLabel="t"/>
        <Key android:codes="121" android:keyLabel="y"/>
        <Key android:codes="117" android:keyLabel="u"/>
        <Key android:codes="105" android:keyLabel="i"/>
        <Key android:codes="111" android:keyLabel="o"/>
        <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left"/>
        <Key android:codes="115" android:keyLabel="s"/>
        <Key android:codes="100" android:keyLabel="d"/>
        <Key android:codes="102" android:keyLabel="f"/>
        <Key android:codes="103" android:keyLabel="g"/>
        <Key android:codes="104" android:keyLabel="h"/>
        <Key android:codes="106" android:keyLabel="j"/>
        <Key android:codes="107" android:keyLabel="k"/>
        <Key android:codes="108" android:keyLabel="l"/>
        <Key android:codes="64" android:keyLabel="\@" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="-1" android:keyLabel="^" android:keyEdgeFlags="left" android:isSticky="true"/>
        <Key android:codes="122" android:keyLabel="z"/>
        <Key android:codes="120" android:keyLabel="x"/>
        <Key android:codes="99" android:keyLabel="c"/>
        <Key android:codes="118" android:keyLabel="v"/>
        <Key android:codes="98" android:keyLabel="b"/>
        <Key android:codes="110" android:keyLabel="n"/>
        <Key android:codes="109" android:keyLabel="m"/>
        <Key android:codes="46" android:keyLabel="."/>
        <Key android:codes="35" android:keyLabel="\#" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="44" android:keyLabel="," android:keyWidth="10%p"  android:keyEdgeFlags="left"/>
        <Key android:codes="47" android:keyLabel="/" android:keyWidth="10%p" />
        <Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true"/>
        <Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true"/>
        <Key android:codes="-10" android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

答案 1 :(得分:1)

我们有两种隐藏键盘的方法:

1:您可以将其设置为在活动

时始终隐藏
android:windowSoftInputMode="stateHidden|stateAlwaysHidden"

2:您可以使用java代码隐藏键盘

public static void hideKeyboardMachine(Activity activity) {
    InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view != null) {
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

我希望它对你有帮助:))