2011-06-28 9 views
0

フォームのように動作するカスタムの編集テキストがあります。ラベル、編集テキスト、ボタンがあります。私はレイアウトにそれを含めるために、フォームを繰り返し使用できるようにタグを使用しています。ボタンにしたい機能は、クリックするとフォームを再現することです。これは、最初のフォームの最初のクリックで可能です。しかし、私が新しいフォームのボタンをクリックすると、何の処置もありません。しかし、最初のフォームボタンをクリックすると、新しいフォームが作成されます。これは私のXMLです:私はそれを含みます。一般的にonClickListenerをinflated xmlボタンに設定しました

<?xml version="1.0" encoding="utf-8"?> 

<TextView android:id="@+id/form_textview" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:textSize="14sp" android:text="Number:" /> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText android:id="@+id/form_edittext" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton android:id="@+id/form_button" 
     android:layout_width="wrap_content" android:layout_height="match_parent" 
     android:src="@drawable/ic_menu_example" android:paddingLeft="1dp" 
     android:paddingRight="1dp" /> 
</LinearLayout> 

そして、これは私が動的に新しいフォームを追加しようとしている方法です:

public final void onClick(final View view) 
{ 

    if (view == findViewById(R.id.form_button)) 
    { 
     try{ 
      LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll); 

       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View tempView = vi.inflate(R.layout.form, null); 
       numberEntryViews.add(tempView); 

       layout.addView(tempView); 
       ImageButton btn = (ImageButton) tempView.findViewById(R.id.form_button); 

       btn.setOnClickListener(this); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       Log.d(TAG, "Failed to inflate"); 
      } 
     } 
} 

私はこれを実装する際のさまざまな方法を試してみましたが、私は持っています運がない。どんな助けや提案も大変感謝しています。

答えて

1

あなたが新たに作成されたボタンに固定されたIDを割り当てることができ

if (view == findViewById(R.id.form_button)) 

新しく作成されたボタンをクリックするか、オフセットで作業しようとした場合、この行は本当ではありませんが

0

私は私が持っていた考え出しクラスレベルのボタンを作成し、新しいフォームからボタンの機能を更新するために表示します。

 try{ 
     LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll); 
     LinearLayout layout2 = (LinearLayout) layout.findViewById(R.id.numbersll); 

     LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     tempView = vi.inflate(R.layout.form, null); 
     numberEntryViews.add(tempView); 

     layout2.addView(tempView); 
     btn = (ImageButton) tempView.findViewById(R.id.form_button); 
     //TODO: Work from here. Ask on Stack Overflow. 
     btn.setOnClickListener(this); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     Log.d(TAG, "Failed to inflate"); 
    } 

btnはクラスレベルであり、クリックする度に更新されます。私の場合は最後のボタンから機能を削除します。

関連する問題