按下对话框中的按钮后,在对话框中插入新的TextView

时间:2014-11-24 19:26:22

标签: android dialog textview

我有一个自定义对话框,它有一个textview和一个edittext和2个按钮。每次用户按下“添加”按钮时,我都会尝试创建一个新的textview,其中包含edittext的文本,并在按钮下方找到它。我怎样才能做到这一点?这是我试过的:

ingDlgBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        //showDialog(INGS_DIALOG);

        final Dialog customIngsDlg = new Dialog(context);
        customIngsDlg.setContentView(R.layout.custom_ings_dialog);
        customIngsDlg.setTitle("Ingredients");
        TextView ingsDlgTitle = (TextView)findViewById(R.id.ingsDialogTitleTv);
        final EditText ingsEt = (EditText)customIngsDlg.findViewById(R.id.ingsDialogEt);
        final Button ingsAddBtn = (Button)customIngsDlg.findViewById(R.id.ingsAddButton);
        final Button ingsFinishBtn = (Button)findViewById(R.id.ingsFinishButton);

        ingsAddBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // ArrayList<String> ingsTmp = new ArrayList<String>();
                String ingredient = ingsEt.getText().toString();
                ingredients.add(ingredient);
                ingsEt.setText("");
                TextView ingItemTv = new TextView(customIngsDlg.getContext());
                ingItemTv.setText(ingredient);
                RelativeLayout layout = new RelativeLayout(OwnRecipeAdding.this);
                RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                parms.addRule(RelativeLayout.BELOW,ingsEt.getId());
                layout.setLayoutParams(parms);
                customIngsDlg.addContentView(ingItemTv,parms);

            }
        });

        //  ingsFinishBtn.setOnClickListener(new View.OnClickListener() {
        //
        //      @Override
        //      public void onClick(View v) {
        //
        //          customIngsDlg.dismiss();
        //      }
        //  });

        customIngsDlg.show();
    }
});

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

我建议将文本视图添加到.xml文件中的自定义布局中,然后在按下添加按钮时将文本添加到该文本视图中。

这需要一些造型,但它的功能。当列表变得太长时,你可能需要一个滚动视图。

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dialog_textview_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="Add Ingredients" />

    <EditText
        android:id="@+id/dialog_edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />

    <LinearLayout
        android:id="@+id/button_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button_add"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add" />

        <Button
            android:id="@+id/button_finish"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Finish" />
    </LinearLayout>

    <TextView
        android:id="@+id/dialog_textview_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="" />

</LinearLayout>

DialogFragment类:

public class TestDialogFragment extends DialogFragment {

   private Button buttonAdd;
   private Button buttonFinish;
   private TextView addTextView;
   private EditText editText;

   public Dialog onCreateDialog(Bundle savedInstanceState) {
      LayoutInflater inflater = getActivity().getLayoutInflater();
      View dialoglayout = inflater.inflate(R.layout.dialog_fragment, null);

      buttonAdd = (Button)dialoglayout.findViewById(R.id.button_add);
      buttonFinish = (Button)dialoglayout.findViewById(R.id.button_finish);
      editText = (EditText)dialoglayout.findViewById(R.id.dialog_edittext);
      addTextView = (TextView)dialoglayout.findViewById(R.id.dialog_textview_2);

      buttonAdd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String text = addTextView.getText().toString();
            text = text + "\n" + editText.getText().toString();
            addTextView.setText(text);
            editText.setText("");
        }

      });

      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setView(dialoglayout)
             .setTitle("Ingredients")
             .setCancelable(false)
             .setInverseBackgroundForced(true);

      AlertDialog dialog = builder.create();

      return dialog;
       }


}