同一片段的多个实例,按钮会影响其他片段

时间:2016-07-07 21:51:44

标签: android android-fragments

很抱歉,我是Android的新手,不知道应该包括什么。

我有一个名为FragmentNumeric的片段,我用XML创建:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        card_view:cardElevation="4dp"
        android:layout_marginBottom="10dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/numeric_fragment_title"
                android:id="@+id/nameText"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:id="@+id/valueText"
                android:text="@string/numeric_default_text"
                android:textAlignment="center"
                android:layout_below="@id/nameText"
                android:layout_centerHorizontal="true"/>

            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/numeric_down_button_text"
                android:id="@+id/downButton"
                android:layout_below="@+id/nameText"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_toLeftOf="@+id/valueText"
                android:layout_toStartOf="@+id/valueText" />

            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/numeric_up_button_text"
                android:id="@+id/upButton"
                android:layout_alignTop="@+id/valueText"
                android:layout_toRightOf="@+id/valueText"
                android:layout_toEndOf="@+id/valueText"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

点击FragmentNumeric时,我可以向LinearLayout添加FloatingActionButton的实例:

final LinearLayout fragContainer = (LinearLayout) findViewById(R.id.llFragmentContainer);

        final LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setId(1);
        fragContainer.addView(ll);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fragTag = "numericField" + numericFragmentCount;
                getFragmentManager().beginTransaction().add(ll.getId(), FragmentNumeric.newInstance("Numeric Field", 0,0,20,2), fragTag ).commit();
                numericFragmentCount++;
            }
        });

这是我的FragmentNumeric.java课程:

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

    public class FragmentNumeric extends Fragment{

        private static TextView nameText;
        private static EditText valueText;
        private static Button upButton;
        private static Button downButton;
        private int value = 0;
        private int min = 0;
        private int max = 10;
        private int increment = 1;
        private String name = "Numeric Field";


        public static FragmentNumeric newInstance(String name, int startVal, int min, int max, int increment) {

            FragmentNumeric f = new FragmentNumeric();

            Bundle b = new Bundle();
            b.putInt("startVal", startVal);
            b.putInt("min", min);
            b.putInt("max", max);
            b.putInt("increment", increment);
            b.putString("name", name);
            f.setArguments(b);
            return f;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_numeric, container, false);

            valueText = (EditText) view.findViewById(R.id.valueText);
            nameText = (TextView) view.findViewById(R.id.nameText);
            upButton = (Button) view.findViewById(R.id.upButton);
            downButton = (Button) view.findViewById(R.id.downButton);

            this.setValue(getArguments().getInt("startVal"));
            this.setIncrement(getArguments().getInt("increment"));
            this.setMin(getArguments().getInt("min"));
            this.setMax(getArguments().getInt("max"));
            this.setName(getArguments().getString("name"));

            valueText.addTextChangedListener(
                    new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            value = Integer.parseInt(valueText.getText().toString());
                        }
                    }
            );

            upButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (value < max) {
                                value+=increment;
                            }
                            setValue(value);
                        }
                    }
            );

            downButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (value > min){
                                value -= increment;
                            }
                            setValue(value);
                        }
                    }
            );



            return view;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
            valueText.setText(""+value);
        }

        public int getMin() {
            return min;
        }

        public void setMin(int min) {
            this.min = min;
        }

        public int getMax() {
            return max;
        }

        public void setMax(int max) {
            this.max = max;
        }

        public int getIncrement() {
            return increment;
        }

        public void setIncrement(int increment) {
            this.increment = increment;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
            nameText.setText(name);
        }
    }

每次按下按钮时,FragmentNumeric成功添加LinearLayout个实例,但每个片段上的upButtondownButton按钮似乎只会影响最新添加的片段。

All 6 buttons only affect the last EditText view.

1 个答案:

答案 0 :(得分:1)

您的按钮是静态的。这就是为什么。他们不需要是静态的。

所以改变

private static TextView nameText;
private static EditText valueText;
private static Button upButton;
private static Button downButton;

private TextView nameText;
private EditText valueText;
private Button upButton;
private Button downButton;
相关问题