动态地想从管理端更新编辑文本中的文本,并且应该在用户端反映更新的文本

Dynamically want to update the text in edit Text from admin end and should reflect updated text in user end

我为我的一个项目创建了 3 个片段..这是其中之一。所以基本上我想要当管理员(aAccountFragment)点击更新报价按钮时,报价应该更新并且应该反映在用户端的 AccountFragment.xml 文件中。

为此,在管理员(aAccountFragment)方面,我提供并编辑了文本,管理员可以在其中每隔 24hrs.and 在用户端(AccountFragment)更新报价,用户只能在任何时候看到更新的报价管理员将更新报价,因为在用户端提供了 textview,在管理端提供了 EditText。

为此,我附上了我的 aAccountFragment.xml、AccountFragment.xml、AccountFragment.java、aAccountFragment.java 代码。

这是我的AccountFragment.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".aAccountFragment"
    android:background="@drawable/ic_launcher_background">

    <!-- TODO: Update blank fragment layout -->


    <TextView
        android:layout_width="339dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="41dp"
        android:layout_marginBottom="465dp"
        android:text="    QUOTE OF THE DAY"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <EditText
        android:layout_width="323dp"
        android:layout_height="111dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="21dp"
        android:layout_marginBottom="273dp"
        android:text="-She decided to start living the life she imagined-"
        android:textColor="@color/DarkRed"
        android:textSize="25sp" />

    <Button
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="107dp"
        android:layout_marginBottom="177dp"
        android:background="@color/LightBlue"
        android:text="UPDATE QUOTE"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <Button
        android:id="@+id/logout1"
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="43dp"
        android:layout_marginBottom="75dp"
        android:background="@color/Pink"
        android:text="LOG OUT"
        android:textColor="@color/black"
        android:textSize="20sp" />

</RelativeLayout>

这是我的AccountFragment.java代码。

public class aAccountFragment extends Fragment {
    Button logout1;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.afragment_account, container, false);

        logout1 = view.findViewById(R.id.logout1);
        logout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), aloginpage.class));
                Toast.makeText(getActivity().getApplicationContext(), "SUCCESSFULLY LOGOUT", Toast.LENGTH_SHORT).show();

            }


        });

        return view;
    }

}

这是我的 AccountFragment.xml 代码。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".AccountFragment"
    android:background="@drawable/ic_launcher_background">

    <!-- TODO: Update blank fragment layout -->


    <TextView
        android:layout_width="339dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="41dp"
        android:layout_marginBottom="465dp"
        android:text="    QUOTE OF THE DAY"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <TextView
        android:layout_width="296dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="49dp"
        android:layout_marginBottom="272dp"
        android:text="-She decided to start living
                               the life she imagined-"
        android:textColor="@color/DarkRed"
        android:textSize="25sp" />

    <Button

        android:id="@+id/logout"
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="28dp"
        android:layout_marginBottom="75dp"
        android:background="@color/Pink"
        android:text="LOG OUT"
        android:textColor="@color/black"
        android:textSize="20sp" />

</RelativeLayout>
    

这是我的 AccountFragment.java 代码。

public class AccountFragment extends Fragment {

    Button logout;

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



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_account, container, false);

        logout = view.findViewById(R.id.logout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), loginpage.class));
                Toast.makeText(getActivity().getApplicationContext(), "SUCCESSFULLY LOGOUT", Toast.LENGTH_SHORT).show();

            }


        });

        return view;
    }
}

第 1 步:AdminFragment.java 中获取来自 edittext 的报价输入。我没有发布它的 xml 文件,因为除了 EditText 之外没有什么重要的。你可以把它放在任何 Activity.

里面

AdminFragment.java

    public class AdminFragment extends Fragment {

    public static AdminFragment newInstance() {
        return new AdminFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
        EditText quoteEditText = view.findViewById(R.id.quote_et);
        Button quoteBtn = view.findViewById(R.id.quote_btn);
        quoteBtn.setOnClickListener(v -> {
            String quote = quoteEditText.getText().toString();
            if (quote.isEmpty()){
                Toast.makeText(getContext(),"Please Enter Quote!",Toast.LENGTH_SHORT).show();
            }else {
                Intent intent = new Intent(getActivity(), BottomNavigationActivity.class);
                intent.putExtra("quote",quote);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}

第 2 步: 正如您声明的 BottomNavigationActivity 那样,它有 3 个片段,即 SnapFragment.javaHomeFragment.javaAccountFragment.java。单击 BottomNavigationActivity 内的 updateQuote 按钮时,将从 AdminFragment.java 接收报价文本。在 BottomNavigationActivity 中收到 quote 后,我将打开第 3 个片段,即 AccountFragment 以显示引用的文本。

BottomNavigationActivity.java

    public class BottomNavigationActivity extends AppCompatActivity {

    String updatedQuote = "";
    BottomNavigationViewModel bottomNavigationViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation);
        bottomNavigationViewModel = new ViewModelProvider(this).get(BottomNavigationViewModel.class);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_snap, R.id.navigation_home, R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        updatedQuote = getIntent().getStringExtra("quote");
        Log.d("tisha==>>","Quote = "+updatedQuote);
        bottomNavigationViewModel.setQuoteLiveData(updatedQuote);

       // Below I am selecting 3rd Fragment to show Quote
        navView.setSelectedItemId(R.id.navigation_account);
    }

}

activity_bottom_navigation.xml

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我在这里使用 ViewModel,因为将 quote 数据从当前 activity 传递到 AccountFragment.java 会更容易,因为 AccountFragment 未实例化通过它的构造函数。 BottomNavigationViewModel 负责存储 quote 并在需要的地方提供。

BottomNavigationViewModel.java

public class BottomNavigationViewModel extends ViewModel {
    private final MutableLiveData<String> quoteLiveData;
    public String getQuoteLiveData(){
        return quoteLiveData.getValue();
    }
    public void setQuoteLiveData(String quote){
        quoteLiveData.setValue(quote);
    }
    public BottomNavigationViewModel(){
        quoteLiveData = new MutableLiveData<>();
    }
}

第 3 步: 现在在 AccountFragment 中,我只得到在 BottomNavigationActivity 中创建的 view model 并从中收集 quote 并将其显示在 TextView.

AccountFragment.java

public class AccountFragment extends Fragment {


    private String mQuote;
    private BottomNavigationViewModel bottomNavigationViewModel;
    public AccountFragment() {
        // Required empty public constructor
    }

    public static AccountFragment newInstance(String quote) {
        return new AccountFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bottomNavigationViewModel = new ViewModelProvider(requireActivity()).get(BottomNavigationViewModel.class);
        mQuote = bottomNavigationViewModel.getQuoteLiveData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_account, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
        TextView quoteView = view.findViewById(R.id.quote_tv);

        quoteView.setText(mQuote);
    }
}