2014-06-06 128 views
0

我有屏幕與列表視圖,當我點擊一個項目,我用EditText替換Textview,通過顯示&隱藏。我可以隱藏TextView &可見的EditText。但是在看到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 ");

請幫幫我。

+0

你不應該處理的EditText上onClick事件,Android的應擡高鍵盤默認情況下。嘗試擺脫你的'editText.setOnClickListener(...' – zgc7009

+0

@ zgc7009,我試過,但沒有任何工作。 – user2060383

+0

你可以發佈你的清單嗎?也許你需要改變這個屬性在活動標籤內: 'android:windowSoftInputMode =「stateVisible」' –

回答

0

更改您的編輯文本的這個屬性,它應該顯示軟鍵盤:

android:focusableInTouchMode="true" 
+0

已經嘗試過這個,但沒有工作 – user2060383

0

類型這個屬性在你Activity tags裏面你manifest

android:windowSoftInputMode="stateVisible"

更多關於此屬性here

Ex充足的

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

在我使用此代碼塊的應用程序:

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

我試過了,這已經是這樣了。 – user2060383

相關問題