0

我正在嘗試在對話框中添加edittext,但是當我嘗試在對話框中添加edittext時,它會顯示在每個新編輯文本的上方。什麼我得到這個.... dialogue getting 在對話框中添加動態編輯文本

但我想這樣.....
dialogue needed

當我點擊添加圖片按鈕,然後新的TextView應該在屏幕中添加。我寫了如下代碼:

final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
add.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     try { 
      Log.w("Addfriendsactivity", "friendsCount--first time-> "+friendsCount); 
     friendsCount++; 
     EditText textview = new EditText(context); 
     Log.w("Addfriendsactivity", "friendsCount---> "+friendsCount); 
     textview.setId(friendsCount); 
     params.addRule(RelativeLayout.BELOW, friendsCount--); 
     friendsCount += 1; 
     Log.w("Addfriendsactivity", "friendsCount--third time-> "+friendsCount); 
     textview.setLayoutParams(params); 
     textview.setVisibility(View.VISIBLE); 
     Log.w("Addfriendsactivity", "params---> "+params); 
     Log.w("Addfriendsactivity", "textview---> "+textview); 
     Log.w("Addfriendsactivity", "relativelayout--> "+textview); 
     dialog.addContentView(textview ,params); 
     textview.setText(String.valueOf(friendsCount)); 
     Log.d("addFriendsActivity", "ashish comes here "); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    }); 
} 

任何人都可以幫助我解釋爲什麼發生這種情況如何從這個問題出來?
在此先感謝..

編輯

我的XML佈局如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    tools:context=".AddFriendsCustomActivity" > 

<EditText 
    android:id="@+id/writename" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="24dp" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/submit_dialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/writename" 
    android:layout_below="@+id/add" 
    android:layout_marginRight="46dp" 
    android:text="Submit" /> 

<ImageView 
    android:id="@+id/add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/writename" 
    android:layout_marginLeft="22dp" 
    android:layout_toRightOf="@+id/writename" 
    android:contentDescription="Add Friend" 
    android:src="@drawable/add" /> 

</RelativeLayout> 
+0

與垂直方向的,而不是RelativeLayou實現的LinearLayout。試試這個 –

+0

@MD你能告訴哪裏和什麼鏈接? – Ashishsingh

回答

0

廣場這條線 - 最終RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT ,LayoutParams.WRAP_CONTENT);按鈕的點擊event.See這裏面:

btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      Log.w("Addfriendsactivity", "friendsCount--first time-> " 
        + friendsCount); 
      friendsCount++; 
      EditText textview = new EditText(MainActivity.this); 
      Log.w("Addfriendsactivity", "friendsCount---> " + friendsCount); 
      if (friendsCount == 2) { 
       textview.setId(friendsCount); 
      } else { 
       textview.setId(friendsCount); 
       params.addRule(RelativeLayout.BELOW, textview.getId() - 1); 
      } 

      // friendsCount += 1; 
      Log.w("Addfriendsactivity", "friendsCount--third time-> " 
        + (textview.getId() - 1)); 
      textview.setLayoutParams(params); 
      textview.setVisibility(View.VISIBLE); 
      Log.w("Addfriendsactivity", "params---> " + params); 
      Log.w("Addfriendsactivity", "textview---> " + textview); 
      Log.w("Addfriendsactivity", "relativelayout--> " + textview); 
      dialog.addView(textview); 
      textview.setText(String.valueOf(friendsCount)); 

      Log.d("addFriendsActivity", "ashish comes here "); 
     } 
    }); 
+0

不錯的解決方案........ –

+0

@Andim它不工作... – Ashishsingh

0

我只是創建例子創建EditTextButton用戶點擊。檢查出來並按照您的要求進行更新。

activity_main.xml中

<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" 
    tools:context=".MainActivity" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:onClick="ShowDialog" 
     android:text="Click Me" /> 

</RelativeLayout> 

layout_row.xml(對於對話框佈局)

​​

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void ShowDialog(View v) { 

     Toast.makeText(getApplicationContext(), "Dialog!", Toast.LENGTH_LONG) 
       .show(); 
     final Dialog dialog = new Dialog(MainActivity.this); 
     dialog.setContentView(R.layout.layout_row); 
     dialog.setTitle("Title..."); 
     final LinearLayout layoutmain = (LinearLayout) dialog 
       .findViewById(R.id.layoutmain); 
     Button btnadd = (Button) dialog.findViewById(R.id.btnadd); 
     btnadd.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       EditText myedittext = new EditText(MainActivity.this); 
       layoutmain.addView(myedittext); 
      } 
     }); 
     dialog.show(); 
    } 

} 

輸出:

enter image description here enter image description here

+0

它顯示我在add視圖方法期間在relai上的空指針錯誤 – Ashishsingh