简体   繁体   中英

Android Studio - Always show keyboard?

I'm attempting to create a simple text adventure type game as a project to help me learn stuff. For this I need the keyboard to always be visible and focused on the only Edit Text on screen (userinputEditText).

The user types something in, presses enter and the text is captured in a string which is then displayed in a Text View (mainTextView), which is inside a ScrollView so that the text always shows the latest entry. A key listener is used to detect when the enter key is pressed to avoid it just adding a new line to the Edit Text.

When you press enter though the keyboard closes and you have to press the Edit Text again to type. I know there's a simple solution to keep the keyboard open permanently but I can't get anything I've tried to work.

    final EditText userinputEditText = (EditText) findViewById(R.id.user_input_edittext);
    final TextView mainTextView = (TextView) findViewById(R.id.mainTextView);
    final ScrollView scrollview = (ScrollView) findViewById(R.id.scrollView);
    scrollview.fullScroll(ScrollView.FOCUS_DOWN);

    /// Update MainText window when Enter is pressed
    userinputEditText.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        String userInputString = userinputEditText.getText().toString();
                        mainTextView.append(userInputString + "\n");
                        userinputEditText.setText("");
                        scrollview.fullScroll(ScrollView.FOCUS_DOWN);
                        return true;
                    default:
                        break;
                }
            }
            return false;
        }
    });

}

Below is the sample code which you could re-use,

In the AndroidManifest.xml use this..

android:windowSoftInputMode="stateAlwaysVisible"

like below

<activity android:name=".MainActivity"
          android:windowSoftInputMode="stateAlwaysVisible">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

In your layout you can use

    <EditText
        android:id="@+id/editText"
        android:singleLine="true"
        android:imeOptions="actionDone"
        ... 
     />

In OnCreate() method of your activity, you could implement something like this...

final EditText editText = ((EditText) findViewById(R.id.editText));
        editText.setOnEditorActionListener(new EditText.OnEditorActionListener(){
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    String value = editText.getText().toString();
                    //TODO .. write your respective logic to add data to your textView

                    editText.setText(""); // clear the text in your editText
                    return true;

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM