【问题标题】:How to open keyboard in editText?如何在editText中打开键盘?
【发布时间】:2014-06-06 12:57:56
【问题描述】:

我有一个带有列表视图的屏幕,当我点击一个项目时,我通过显示和隐藏将 Textview 替换为 EditText。我能够隐藏 TextView 并显示 EditText。但是在 EditText 可见后,它不像正常行为那样显示软键盘。我已经尝试过以编程方式显示键盘的其他答案,但根本没有任何效果。

这是我的 list.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:background="@color/settings_list_item"
    android:orientation="vertical"
    tools:context=".Setting" >

    <TextView
        android:id="@+id/tvHeader"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/gradient_bg_hover_holo"
        android:gravity="center"
        android:text="@string/settingTitle"
        android:textColor="@color/white_smoke"
        android:textSize="22sp" />

    <ListView
        android:id="@+id/list_settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants" >
    </ListView>

</LinearLayout>

这是我的 list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/settings_list_item"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:contentDescription="@null"
        android:src="@drawable/ic_settings_1" />

    <TextView
        android:id="@+id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/list_image"
        android:text="@string/settings_item_1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/txtSelfDestroyCount"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/list_image"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:hint="@string/selfDestoryCount"
        android:imeOptions="actionDone"
        android:inputType="numberSigned"
        android:lines="1"
        android:maxLength="1"
        android:maxLines="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/rightArrow"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:contentDescription="@null"
        android:src="@drawable/ic_arrow" />

    <ToggleButton
        android:id="@+id/masterPasswordOption"
        android:layout_width="60dip"
        android:layout_height="50dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/master_password"
        android:textColor="#000000"
        android:visibility="gone" />

</RelativeLayout>

这是隐藏 textView 并显示 editText 的代码。

case 3:
{
    TextView textView = ( TextView ) view.findViewById( R.id.txtItemName );
    textView.setVisibility( View.INVISIBLE );
    ImageView imageView = ( ImageView ) view.findViewById( R.id.rightArrow );
    imageView.setVisibility( View.INVISIBLE );
    final EditText editText = ( EditText ) view.findViewById( R.id.txtSelfDestroyCount );
    editText.setVisibility( View.VISIBLE );
    editText.setFocusableInTouchMode( true);
    editText.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            System.out.println ( "clicked " );
            // Set keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            // Lets soft keyboard trigger only if no physical keyboard present
            imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
    });     

    adapter.notifyDataSetChanged();
   }

当我点击 EditText 时,我可以在 DDMS 上看到 System.out.println ( "clicked " );

请帮帮我。

【问题讨论】:

  • 您不必为 EditText 处理 onClick 事件,Android 应该默认为键盘充气。尝试摆脱你的editText.setOnClickListener(...
  • @zgc7009,我试过了,但没有任何效果。
  • 您能发布您的清单吗?也许您需要在 Activity 标记中更改此属性:android:windowSoftInputMode="stateVisible"
  • @LuisLavieri 我也试过那个部分,但根本没用

标签: android android-listview android-edittext android-keypad


【解决方案1】:

更改编辑文本的这个属性,它应该会显示软键盘:

android:focusableInTouchMode="true"

【讨论】:

    【解决方案2】:

    Activity tags 中的manifest 中输入此属性

    android:windowSoftInputMode="stateVisible"

    更多关于这个属性here:

    示例

    <activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
    

    【讨论】:

      【解决方案3】:

      在一个应用程序中我使用这个代码块:

      final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多