我怎样才能删除在创建时自动出现的键盘?

时间:2016-10-03 06:38:23

标签: android keyboard

我有自定义键盘或小键盘的编辑文本我的问题是当我打开我的应用程序时,Android键盘会自动出现我不希望这种情况发生 当我点击编辑文本我的自定义键盘出现时,我的应用程序想法很简单。 所以我怎么能删除第一个出现的Android键盘 顺便说一句,如果我在应用程序中手动取消它,它永远不会出现,但正如我说重新打开应用程序再次出现请帮助

enter image description here

enter image description here

public class BasicOnKeyboardActionListener implements OnKeyboardActionListener {

private Activity mTargetActivity;

/***
 * 
 * @param targetActivity
 *            Activity a cui deve essere girato l'evento
 *            "pressione di un tasto sulla tastiera"
 */
public BasicOnKeyboardActionListener(Activity targetActivity) {
    mTargetActivity = targetActivity;
}

@Override
public void swipeUp() {
    // TODO Auto-generated method stub

}

@Override
public void swipeRight() {
    // TODO Auto-generated method stub

}

@Override
public void swipeLeft() {
    // TODO Auto-generated method stub

}

@Override
public void swipeDown() {
    // TODO Auto-generated method stub

}

@Override
public void onText(CharSequence text) {
    // TODO Auto-generated method stub

}

@Override
public void onRelease(int primaryCode) {
    // TODO Auto-generated method stub

}

@Override
public void onPress(int primaryCode) {
    // TODO Auto-generated method stub

}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    long eventTime = System.currentTimeMillis();
    KeyEvent event = new KeyEvent(eventTime, eventTime,
            KeyEvent.ACTION_DOWN, primaryCode, 0, 0, 0, 0,
            KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);

    mTargetActivity.dispatchKeyEvent(event);
}

CustomKeyboardView.class

public class CustomKeyboardView extends KeyboardView {

public CustomKeyboardView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void showWithAnimation(Animation animation) {
    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setVisibility(View.VISIBLE);
        }
    });

    setAnimation(animation);
}

KeyboardWidgetTutorialActivity.class

public class KeyboardWidgetTutorialActivity extends Activity {

private CustomKeyboardView mKeyboardView;
private View mTargetView;
private Keyboard mKeyboard;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mKeyboard = new Keyboard(this, R.xml.keyboard);
    mTargetView = (EditText) findViewById(R.id.target);
    mTargetView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Dobbiamo intercettare l'evento onTouch in modo da aprire la
            // nostra tastiera e prevenire che venga aperta quella di
            // Android
            showKeyboardWithAnimation();
            return true;
        }
    });

    mKeyboardView = (CustomKeyboardView) findViewById(R.id.keyboard_view);
    mKeyboardView.setKeyboard(mKeyboard);
    mKeyboardView
            .setOnKeyboardActionListener(new BasicOnKeyboardActionListener(
                    this));
}

/***
 * Mostra la tastiera a schermo con una animazione di slide dal basso
 */
private void showKeyboardWithAnimation() {
    if (mKeyboardView.getVisibility() == View.GONE) {
        Animation animation = AnimationUtils
                .loadAnimation(KeyboardWidgetTutorialActivity.this,
                        R.anim.slide_in_bottom);
        mKeyboardView.showWithAnimation(animation);
    }
}

这是主要的xml

<RelativeLayout android:id="@+id/LinearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/container" android:layout_alignParentTop="true"
    android:layout_height="fill_parent" android:layout_above="@+id/keyboard_view">
    <EditText android:layout_width="fill_parent" android:id="@+id/target"
        android:layout_height="wrap_content" />
</LinearLayout>

<it.anddev.tutorial.CustomKeyboardView
    android:id="@+id/keyboard_view" android:visibility="gone"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"></it.anddev.tutorial.CustomKeyboardView>

9 个答案:

答案 0 :(得分:1)

添加新类Utilities.java包含以下代码

public static void hideKeypad(Context context, View edit) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}

然后调用以下代码来隐藏键盘

Utilities.hideKeypad(thisActivity,txtName);

示例:

txtName - &gt;你可以在这里传递你的Edittext变量

答案 1 :(得分:0)

就这样做

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

答案 2 :(得分:0)

这将有助于

            InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);

答案 3 :(得分:0)

我之前使用过以下代码来解决此问题:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

onCreate()

上使用此功能

答案 4 :(得分:0)

您可以通过在相应的活动代码中添加以下行来隐藏您在Manifest文件中的活动键盘:

<activity android:name="packageName.ActivityName"
  android:windowSoftInputMode="stateHidden"  />

答案 5 :(得分:0)

在线性布局中添加以下代码行

android:focusable="true">

答案 6 :(得分:0)

您只需在清单文件中定义该代码。

<activity android:name=".activity.HomeScreen"
          android:windowSoftInputMode="stateAlwaysHidden"/>

希望这能帮到你..

答案 7 :(得分:0)

创建此方法,只需从onCreate()调用它:

private void hideSoftKeyBoard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

    if(imm.isAcceptingText()) { // verify if the soft keyboard is open                      
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

答案 8 :(得分:0)

您可以添加

android:focusable="true" 
android:focusableInTouchMode="true"
android:descendantFocusability="blocksDescendants"

到LinearLayout以从EditText中删除焦点。