有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java主片段与子片段重叠

我正在尝试做一个基本的倒计时应用程序

以下是我的主要活动

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout 
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.dot.timer.MainActivity">


    <Chronometer
    安卓:id="@+id/chronometer"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="108dp"
    安卓:textSize="70sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <fragment
    安卓:id="@+id/fragment"
    安卓:name="com.example.dot.timer.SetTimer"
    安卓:layout_width="0dp"
    安卓:layout_height="0dp"
    安卓:layout_marginBottom="8dp"
    安卓:layout_marginEnd="8dp"
    安卓:layout_marginStart="8dp"
    安卓:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/chronometer" />

    </安卓.support.constraint.ConstraintLayout>

我创建了两个片段

1)设置计时器。java\fragment\u set\u计时器。xmlWith a EditText and Button

2)时间按钮。java \片段\计时器\按钮。xmlThree Buttons

SetTimer活动如下所示

  public class SetTimer extends Fragment{
  View view;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view=inflater.inflate(R.layout.fragment_set_timer, container, false);

    Button button=(Button)view.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft =fm.beginTransaction();
            Fragment fragment=new TimerButtons();
            ft.replace(R.id.fragment,fragment);
            ft.addToBackStack(null);
            ft.commit();
        }
    });

    return view;
}}

setTimerXML如下所示

<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.dot.timer.SetTimer">
<安卓.support.constraint.ConstraintLayout
   安卓:layout_width="match_parent"
   安卓:layout_height="match_parent">
<EditText
    安卓:id="@+id/editText2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:ems="10"
    安卓:inputType="number"
    app:layout_constraintBaseline_toBaselineOf="@+id/button"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    安卓:id="@+id/button"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Set"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/editText2"
    app:layout_constraintTop_toTopOf="parent"
     />
     </安卓.support.constraint.ConstraintLayout>

  </FrameLayout>

当我运行项目并单击Set按钮时 this is what happens

背景框甚至可以点击 但不应该使用。replace()销毁以前的片段

为什么会发生这种情况?我如何纠正它


共 (0) 个答案