Я пытаюсь узнать, как использовать общие предпочтения из видео на YouTube, однако на YouTube используется действие вместо фрагмента, поэтому я пытаюсь использовать общие предпочтения во фрагментах, используя мое понимание представлений. когда я пытаюсь запустить свой код, фрагмент открывается, однако, при нажатии любой из двух кнопок, кажется, ничего не работает.
Вот мой код:
фрагмент_. 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"
android:background="@color/fragmentone">
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="potato"
android:textSize="30sp"
android:gravity="center_horizontal"/>
<EditText
android:id="@+id/write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/display"/>
<Button
android:id="@+id/applychange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Apply"
android:layout_below="@+id/write"
android:layout_centerHorizontal="true"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/applychange"
android:layout_centerHorizontal="true"
android:id="@+id/switchbool"/>
<Button
android:id="@+id/savechange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_centerHorizontal="true"
android:layout_below="@+id/switchbool"/>
</RelativeLayout>
FragmentOne. java:
package com.example.testing;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
View RootView;
private TextView display;
private EditText write;
private Button applychange;
private Switch switchbool;
private Button savechange;
public static final String shared = "shared";
public static final String text = "text";
public static final String switch1 = "switch1";
private String Texting;
private boolean switching;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View RootView = inflater.inflate(R.layout.fragment_one, container, false);
this.RootView = RootView;
display = RootView.findViewById(R.id.display);
write = RootView.findViewById(R.id.write);
applychange = RootView.findViewById(R.id.applychange);
switchbool = RootView.findViewById(R.id.switchbool);
savechange = RootView.findViewById(R.id.savechange);
applychange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.setText(write.getText().toString());
}
});
savechange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
savedata();
}
});
loaddata();
updatedata();
return inflater.inflate(R.layout.fragment_one, container, false);
}
public void savedata() {
SharedPreferences sharedPreferences = RootView.getContext().getSharedPreferences(shared, Context.MODE_PRIVATE
);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(text, display.getText().toString());
editor.putBoolean(switch1, switchbool.isChecked());
editor.apply();
Toast.makeText(RootView.getContext(), "Saved", Toast.LENGTH_SHORT).show();
}
public void loaddata() {
SharedPreferences sharedPreferences = RootView.getContext().getSharedPreferences(shared, Context.MODE_PRIVATE);
Texting = sharedPreferences.getString(text, "");
switching = sharedPreferences.getBoolean(switch1, false);
}
public void updatedata() {
display.setText(Texting);
switchbool.setChecked(switching);
}
}