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

1szpjjfi  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(393)


我已经为我的一个项目创建了3个片段…这是其中之一。因此,基本上我希望管理员(aaAccountFragment)在单击更新报价按钮时,报价应该被更新,并且应该反映在用户端的accountfragment.xml文件中。
为此,在管理(aaAccountFragment)端,我提供并编辑了文本,管理员可以每24小时更新一次报价。在用户端(accountfragment),用户只能在管理员更新报价时看到更新的报价,因为在用户端提供了textview,在管理端提供了edittext。
为此,我附上了aaccountfragment.xml、accountfragment.xml、accountfragment.java和aaccountfragment.java代码。
这是我的aaccountfragment.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>

这是我的aaccountfragment.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;
    }
}
k4emjkb1

k4emjkb11#

步骤1:从 edittext 内部 AdminFragment.java . 我不会发帖的 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);
    }
}

第二步:如你所说 BottomNavigationActivity 有3个碎片 SnapFragment.java , HomeFragment.java 以及 AccountFragment.java . 报价文本将从 AdminFragment.java 同时单击 updateQuote 内部按钮 BottomNavigationActivity . 一次 quote 在内部接收 BottomNavigationActivity ,我正在打开第三个片段,即 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);
    }

}

活动\底部\导航.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 从当前活动到 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<>();
    }
}

第三步:现在进去 AccountFragment ,我刚刚得到 view model 它是在内部创建的 BottomNavigationActivity 收集 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);
    }
}

相关问题