如何在editText中打开键盘?

时间:2014-06-06 12:57:57

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

我有listview的屏幕,当我点击一个项目时,我用EditText替换Textview,显示&躲了起来。我能够隐藏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&amp;的代码。显示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 " );

请帮帮我。

3 个答案:

答案 0 :(得分:0)

更改编辑文本的此属性,它应显示软键盘:

android:focusableInTouchMode="true"

答案 1 :(得分:0)

Activity tags

中的manifest中输入此属性

android:windowSoftInputMode="stateVisible"

有关此属性的更多信息here

示例

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

答案 2 :(得分:0)

在应用程序中,我使用此代码块:

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