在视图中添加动态按钮并永久保存

时间:2021-06-30 16:21:43

标签: java android android-layout button android-dialogfragment

我有一个带有此视图的片段:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:tag="general"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#343535"
    android:orientation="vertical"
    tools:context=".fragments.GeneralFragment">

    <Button
        android:id="@+id/hello"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:onClick="onClick"
        android:text="@string/hello" />

    <Button
        android:id="@+id/observed"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:onClick="onClick"
        android:text="@string/observed" />

    <Button
        android:id="@+id/thanks"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:onClick="onClick"
        android:text="@string/thanks" />

</LinearLayout>

有 3 个按钮。每当您单击其中一个时,就会显示其文本。

现在我想动态添加另一个按钮。应该在 @+id/hello 之前添加。

我已经试过了

LinearLayout root = (LinearLayout) view.findViewById(R.id.root);
root.addView();

但它看起来完全错误,因为缺少一些参数。

例如,新按钮应该是这样的:

XML:

<Button
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center_vertical"
    android:onClick="onClick"
    android:text="I am a dynamic text" />

此按钮应永久保存在应用中。我该怎么做?

更新

我正在使用对话框,所以这是类:

@SuppressLint("ValidFragment")
public class Dialog extends DialogFragment {
    private final int _layout;
    private TextInputEditText _customTextField;

    @SuppressLint("ValidFragment")
    public Dialog(int layout) {
        _layout = layout;
    }

    public interface ICustomTts {
        void customTts(String input, Activity activity);
    }
    public ICustomTts iCustomTts;

    public interface  ITarget {
        void getTarget(String input);
    }
    public ITarget iTarget;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(_layout, container, false);

        // Display fragment_custom
        if (_layout == R.layout.fragment_custom) {
            _customTextField = view.findViewById(R.id.customTextField);
            Button _customBtn = view.findViewById(R.id.customCta);
            _customBtn.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View view) {
                    Log.d(TAG, "onClick: Clicked on CTA of custom");
                    String input = _customTextField.getText().toString();
                    if (!input.equals("")) {
                        iCustomTts.customTts(input, getActivity());
                        _dismiss();
                    }
                }
            });
            MaterialCheckBox customeSave = view.findViewById(R.id.customSave);
            customeSave.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "customeSave was clicked!");
                    LinearLayout root = view.findViewById(R.id.root);
                    Button button = new Button(getContext());
                    float heightInPixel = 60 * getResources().getDisplayMetrics().density;
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) heightInPixel);
                    params.gravity = Gravity.CENTER;
                    button.setText("I am a dynamic text");
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // Do the stuff on the click
                        }
                    });

                    root.addView(button, 0);
                }
            });
        }

        // Display fragment_target
        if (_layout == R.layout.fragment_target) {
            _customTextField = view.findViewById(R.id.targetTextField);
            Button _customBtn = view.findViewById(R.id.targetCta);
            _customBtn.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View view) {
                    Log.d(TAG, "onClick: Clicked on CTA of target");
                    String input = _customTextField.getText().toString();
                    if (!input.equals("")) {
                        iTarget.getTarget(input);
                        _dismiss();
                    }
                }
            });
        }
        return view;
    }

    /**
     * Dismissing the dialog
     */
    private void _dismiss() {
        this.dismiss();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if (_layout == R.layout.fragment_custom) {
                iCustomTts = (ICustomTts) getActivity();
            }
            else if (_layout == R.layout.fragment_target) {
                iTarget = (ITarget) getActivity();
            }
        }
        catch (ClassCastException e) {
            Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

需要使用 addView() 的双参数版本来确定 LinearLayout 内按钮的顺序(索引):

LinearLayout root = view.findViewById(R.id.root);
Button button = new Button(requireContext());
float heightInPixel = 60 * getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) heightInPixel);
params.gravity = Gravity.CENTER;
button.setText("I am a dynamic text");
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do the stuff on the click
    }
});

root.addView(button, 0);
相关问题