如何移除在onCreate时自动出现的键盘?

问题描述:

我有自定义键盘或数字键盘的编辑文本我的问题是当我打开我的应用程序Android键盘自动出现我不希望发生这种情况 我的应用程序的想法很简单,当我点击编辑文本我的自定义键盘出现。 所以我怎么能删除的方式,如果我的应用程序手动取消它出现在第一 Android键盘就一直没有出现,但正如我在重新开启应用程式说再次出现,请大家帮忙如何移除在onCreate时自动出现的键盘?

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> 

您EDITTEXT变量您刚才定义清单文件的代码。

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

希望这个帮助你..

+0

感谢您的帮助可以帮助我简单的事情 我想在底部右键是输入或完成按钮,当我按它不去像新的一行完成按钮什么是代码而不是66,当我按下数字我不想要这个白色高光显示或标志 – CJS

+0

你想去完成按钮上的下一个编辑文本或想在同一个edittext文本 – Sarbjyot

+0

年它是这样的,但我没有任何新的编辑文本 – CJS

只是这样做

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

哪一类我应该把这个? – CJS

这将有助于

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

我应该把这个放在哪一类? – CJS

+0

在您的onCreate()在KeyboardWidgetTutorialActivity.class –

+0

无论你的第一个事件是发生和键盘的弹出 –

我用下面这个问题前面的代码:通过添加以下到相应的活动标记线

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

使用这种上onCreate()

+0

哪类我应该把这个? – CJS

+0

在''KeyboardWidgetTutorialActivity'类'onCreate'方法下 – harneev

您可以隐藏键盘清单文件,你的活动:

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

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

android:focusable="true"> 

添加一个新类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的 - >你可以通过这里

创建这个方法并调用它的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); 
    } 
} 

您可以添加

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

到的LinearLayout从EditText上移除焦点。