2014-12-03 66 views
0

美好的一天。Android - 手動充氣列表視圖行上的自定義鍵盤不顯示

我有一個android應用程序。在我的應用程序中,我有一個自定義列表視圖,其中有5列是textViews。用戶可以單擊行,一旦他做了,行佈局將會改變,將最後2個textViews改爲EditTexts。然後,我將新的EditTexts註冊到我的自定義鍵盤taken from this example - 請注意,我爲他的自定義鍵盤類做了一個功能性複製粘貼,以及如何使它在主佈局中工作。但是,當我單擊該行中的EditText時,我的自定義鍵盤根本不顯示。

我有一個全局變量,例如:

CustomKeyboard mCustomKeyboard; 

而在我的活動onCreate()方法,我做的:

mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.custom_keyboard); 

這是我的佈局,我有在底部的KeyboardView的佈局:

<RelativeLayout 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:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".SearchResult" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <!-- A lot of views go here, enclosed in my linear layout --> 

    </LinearLayout> 

    <android.inputmethodservice.KeyboardView 
     android:id="@+id/keyboardview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:visibility="gone" /> 
</RelativeLayout> 

這裏是更改佈局的代碼。我所做的是從舊佈局中獲取行值,獲取新佈局search_result_inflate,然後使用我獲得的值設置新佈局的文本。請注意0​​線膨脹佈局後:

private void changeLayout(final View view){ 

    //get views from old layout 
    TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput); 
    TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput); 
    TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName); 
    TextView textViewPrice = (TextView)view.findViewById(R.id.price); 
    TextView textViewDiscount = (TextView)view.findViewById(R.id.discount); 

    //store values in strings 
    String itemName = textViewName.getText().toString(); 
    String itemPrice = textViewPrice.getText().toString(); 
    String itemDiscount = textViewDiscount.getText().toString(); 
    String itemQty = textViewQuantity.getText().toString(); 
    String itemDisc = textViewDiscountReq.getText().toString(); 

    //set the view to gone 
    textViewQuantity.setVisibility(View.GONE); 
    textViewDiscountReq.setVisibility(View.GONE); 
    textViewName.setVisibility(View.GONE); 
    textViewPrice.setVisibility(View.GONE); 
    textViewDiscount.setVisibility(View.GONE); 

    //get the old layout 
    LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout); 

    //get the inflate/new view 
    View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null); 

    //get the views in the new view, populate them 
    TextView newName = (TextView)child.findViewById(R.id.dialogItemName); 
    newName.setText(itemName); 

    TextView newDiscount = (TextView)child.findViewById(R.id.discount); 
    newDiscount.setText(itemDiscount); 

    TextView newPrice = (TextView)child.findViewById(R.id.price); 
    newPrice.setText(itemPrice); 

    EditText qtyInput = (EditText)child.findViewById(R.id.qtyInputSearchResult); 
    qtyInput.setText(itemQty); 

    EditText discInput = (EditText)child.findViewById(R.id.discInputSearchResult); 
    discInput.setText(itemDisc); 

    //show new layout 
    ll_inflate.removeAllViews(); 
    ll_inflate.removeAllViewsInLayout(); 
    ll_inflate.addView(child); 

    mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult); 
} 

這裏是我的search_result_inflate.xml file

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/search_result_inflate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:minHeight="50dp" 
    android:orientation="horizontal" 
    android:padding="1dip" 
    android:weightSum="1" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:orientation="horizontal" 
     android:padding="1dip" > 

     <TextView 
      android:id="@+id/dialogItemName" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginRight="2dp" 
      android:layout_weight="0.54" 
      android:gravity="center" 
      android:text="Item Name" 
      android:textSize="23sp" /> 

     <TextView 
      android:id="@+id/price" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginRight="2dp" 
      android:layout_weight="0.14" 
      android:gravity="center" 
      android:text="Price" 
      android:textSize="23sp" /> 

     <TextView 
      android:id="@+id/discount" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginRight="2dp" 
      android:layout_weight="0.10" 
      android:gravity="center" 
      android:text="Discount" 
      android:textSize="23sp" /> 

     <EditText 
      android:id="@+id/qtyInputSearchResult" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginRight="2dp" 
      android:layout_weight="0.14" 
      android:background="@layout/edittext_selector" 
      android:gravity="center" 
      android:hint="qtyInput" 
      android:textColorHighlight="@color/white_opaque" 
      android:textSize="23sp" > 
     </EditText> 

     <EditText 
      android:id="@+id/discInputSearchResult" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.11" 
      android:background="@layout/edittext_selector" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:gravity="center" 
      android:hint="discInput" 
      android:textColorHighlight="@color/white_opaque" 
      android:textSize="23sp" /> 
    </LinearLayout> 

</LinearLayout> 

正如你所看到的,我有2個editTexts和我註冊qtyInputSearchResult到自定義鍵盤類。但是,自定義鍵盤不顯示。

我也嘗試在另一個activity中使用editText上的自定義鍵盤類,它工作得很好。我在這裏錯過了什麼嗎?我很困惑,爲什麼自定義鍵盤不能正常顯示。

任何幫助非常感謝,謝謝。

回答

2

明白了,我把鍵盤佈局在search_result_inflate.xml像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/search_result_inflate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:minHeight="50dp" 
    android:orientation="horizontal" 
    android:padding="1dip" 
    android:weightSum="1" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:orientation="horizontal" 
     android:padding="1dip" > 

     <!-- a lot of components here --> 

    </LinearLayout> 

    <android.inputmethodservice.KeyboardView 
     android:id="@+id/keyboardview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:visibility="gone" /> 

</RelativeLayout>