首页 > 解决方案 > 片段不会出现在 Android 中

问题描述

我最近了解了 Android 中的 Fragments,并正在构建一个记事本应用程序来练习它们。

想法:应用程序背后的想法很简单。我按下按钮添加一个新注释。CardView 视图组变为可见。片段将位于此 CardView 中。

问题:当我按下按钮添加新笔记时,片段没有弹出。

MainActivity.java

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Objects;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CardView fragmentCardView;
    private Button newNoteButton;
    private FragmentManager fragmentManager = getSupportFragmentManager();
    private FragmentTransaction fragmentTransaction;

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

        newNoteButton = findViewById(R.id.new_note_button);
        fragmentCardView = findViewById(R.id.fragment_cardView);
    }

    void setActionBar(){
        Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.layout_custom_action_bar);
    }

    void showCreateNoteFragment(){
        CreateNoteFragment createNoteFragment = CreateNoteFragment.newInstance();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_cardView, createNoteFragment);
        fragmentTransaction.commit();
        fragmentCardView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v == newNoteButton)
            showCreateNoteFragment();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F2F2F2"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/notes_listView"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/new_note_button"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:layout_marginTop="27dp"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/new_note_button_string"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/notes_listView"/>

    <androidx.cardview.widget.CardView
        android:id="@+id/fragment_cardView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="100dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:visibility="invisible"/>

</androidx.constraintlayout.widget.ConstraintLayout>

创建NoteFragment.java

package com.example.notepadapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class CreateNoteFragment extends Fragment implements View.OnClickListener {

    private EditText inputEditText;

    public CreateNoteFragment() {
        // Required empty public constructor
    }

    static CreateNoteFragment newInstance(){
        return new CreateNoteFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_create_note, container, false);
        inputEditText = rootView.findViewById(R.id.input_editText);
        Button saveButton = rootView.findViewById(R.id.save_button);
        saveButton.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        String saveButtonText = inputEditText.getText().toString();
    }
}

fragment_create_note.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="100dp"
    android:layout_marginBottom="100dp"
    android:layout_marginStart="25dp"
    android:layout_marginEnd="25dp"
    android:background="#F2F2F2"
    tools:context=".CreateNoteFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/header_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="@string/display_fragment_header_string"
            android:layout_marginTop="10dp"
            android:textAlignment="center"
            android:textSize="20sp"
            android:fontFamily="@font/nunito_bold"/>

        <View
            android:id="@+id/divider_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="5dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:layout_below="@id/header_textView"
            android:background="#000000" />

        <EditText
            android:id="@+id/input_editText"
            android:layout_width="match_parent"
            android:layout_height="475dp"
            android:layout_below="@id/divider_view"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="20dp"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:gravity="top"
            android:background="#FFFFFF"
            tools:ignore="Autofill,LabelFor,TextFields" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:text="@string/save_button_string"
            android:fontFamily="@font/nunito_bold"/>

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

谁能检查为什么片段没有弹出?谢谢你们的帮助,伙计们。

PS:我认为由于我使用的所有 ViewGroups,应用程序可能会变得有点臃肿。如果你们有更好的方法来设计这个,无论如何,我会喜欢任何输入。

标签: javaandroidandroid-fragments

解决方案


当我按下按钮添加新笔记时,片段不会弹出。

这是意料之中的,因为您的按钮在按下时不知道该做什么。

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

        newNoteButton = findViewById(R.id.new_note_button);
        // Button will call activity's onClick whenever it's clicked
        newNoteButton.setOnClickListener(this);
        fragmentCardView = findViewById(R.id.fragment_cardView);
    }

推荐阅读