2016-03-03 84 views
0

好吧!所以这个问题有很多答案。但没有一个对我有用。也许我会在某个地方出错。关闭android默认键盘onClick EditText

我想要做的是,我不想当我点击我的EditText时弹出默认的Android软键盘。

这里是我的xml文件

activity_main.xml中

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="0dp" 
     android:layout_marginTop="0dp" 
     android:orientation="horizontal" 
     android:focusableInTouchMode="true" 
     > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="INR" 
      android:textSize="28dp" 
      android:layout_marginTop="12dp" 
      android:layout_marginLeft="55dp" 
      ></TextView> 

     <ImageView 
      android:layout_width="55dp" 
      android:layout_height="55dp" 
      android:layout_marginLeft="15dp" 
      android:src="@drawable/indiaflag" 
      /> 

     <EditText 
      android:id="@+id/secondText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:layout_marginLeft="70dp" 
      android:textSize="28dp" 
      android:text="" 
      /> 
    </LinearLayout> 

下面是隐藏在键盘的代码,我使用InputMethodManager没有显示在键盘上的EditText

的点击
secondText = (EditText) findViewById(R.id.secondText); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.showSoftInput(secondText, InputMethodManager.HIDE_NOT_ALWAYS); 

我在OnCreate中使用此代码只是在secondText声明后,但这不起作用。我甚至尝试了在setOnClickListener上使用上述代码的替代方法。但仍然没有成功。

谁能告诉我我哪里错了? 在此先感谢

回答

0

地址:

secondText.setInputType(InputType.TYPE_NULL); 
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(secondText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); 

而不是

secondText.setInputType(InputType.TYPE_NULL); 

使用

OnTouchListener otl = new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       Layout layout = ((EditText) v).getLayout(); 
       float x = event.getX() + secondText.getScrollX(); 
       int offset = layout.getOffsetForHorizontal(0, x); 
       if(offset>0) 
        if(x>layout.getLineMax(0)) 
         secondText.setSelection(offset);  // touch was at end of text 
        else 
         secondText.setSelection(offset - 1); 
       break; 
       } 
      return true; 
      } 
     }; 
     secondText.setOnTouchListener(otl); 
+0

我应该将它添加到onCreate还是setOnClickListener? –

+0

我尝试了你的建议,它像一个魅力。但光标消失。我应该怎么做光标而不是键盘? –

+0

查看已更新的答案 – Jas

1

使用此功能隐藏keyboared

public static void hide_keyboard(Activity activity) { 
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    View view = activity.getCurrentFocus(); 
    if (view == null) 
     view = new View(activity); 
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
}