回答編集履歴
2
修正
answer
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
----
|
4
4
|
|
5
5
|
回答にしちゃったので、ついでに AndroidStudio のタブのプロジェクト雛形から、タイトルを修正・保存 (SharedPreferences) するコードを作りました。
|
6
|
-
タブは TagLayout 、ビューページャは ViewPager2 、バインディングは嫌いなので外しています^^;
|
6
|
+
タブは TagLayout 、ビューページャは ViewPager2 、ダイアログはフラグメント、バインディングは嫌いなので外しています^^;
|
7
7
|
|
8
8
|
MainActivity.java
|
9
9
|
```java
|
1
コード追加
answer
CHANGED
@@ -1,1 +1,256 @@
|
|
1
|
-
変更をどこかに保存して、再起動時にそれを読んで設定すれば良いのではと思います。
|
1
|
+
変更をどこかに保存して、再起動時にそれを読んで設定すれば良いのではと思います。
|
2
|
+
|
3
|
+
----
|
4
|
+
|
5
|
+
回答にしちゃったので、ついでに AndroidStudio のタブのプロジェクト雛形から、タイトルを修正・保存 (SharedPreferences) するコードを作りました。
|
6
|
+
タブは TagLayout 、ビューページャは ViewPager2 、バインディングは嫌いなので外しています^^;
|
7
|
+
|
8
|
+
MainActivity.java
|
9
|
+
```java
|
10
|
+
package com.teratail.q375388;
|
11
|
+
|
12
|
+
import android.content.SharedPreferences;
|
13
|
+
import android.os.Bundle;
|
14
|
+
import android.view.*;
|
15
|
+
|
16
|
+
import com.google.android.material.tabs.*;
|
17
|
+
|
18
|
+
import androidx.appcompat.app.*;
|
19
|
+
import androidx.lifecycle.ViewModelProvider;
|
20
|
+
import androidx.viewpager2.widget.ViewPager2;
|
21
|
+
|
22
|
+
import com.teratail.q375388.ui.main.*;
|
23
|
+
|
24
|
+
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener, TabTitleChangeDialogFragment.Listener {
|
25
|
+
|
26
|
+
private SharedPreferences sharedPreferences;
|
27
|
+
private TabLayout tabLayout;
|
28
|
+
private PageViewModel pageViewModel;
|
29
|
+
|
30
|
+
@Override
|
31
|
+
protected void onCreate(Bundle savedInstanceState) {
|
32
|
+
super.onCreate(savedInstanceState);
|
33
|
+
setContentView(R.layout.activity_main);
|
34
|
+
|
35
|
+
sharedPreferences = getSharedPreferences("TabTitles", MODE_PRIVATE);
|
36
|
+
|
37
|
+
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
|
38
|
+
pageViewModel.restoreTabTitles(sharedPreferences, new String[]{
|
39
|
+
getString(R.string.tab_text_1), getString(R.string.tab_text_2)
|
40
|
+
});
|
41
|
+
|
42
|
+
ViewPager2 viewPager = findViewById(R.id.pager);
|
43
|
+
viewPager.setAdapter(new SectionsStateAdapter(this));
|
44
|
+
|
45
|
+
tabLayout = findViewById(R.id.tablayout);
|
46
|
+
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> tab.setText(pageViewModel.getTabTitle(position))).attach();
|
47
|
+
|
48
|
+
for(int i=0; i<tabLayout.getTabCount(); i++) {
|
49
|
+
View view = tabLayout.getTabAt(i).view;
|
50
|
+
view.setTag(i);
|
51
|
+
view.setOnLongClickListener(this);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
@Override
|
56
|
+
public boolean onLongClick(View v) {
|
57
|
+
int tabIndex = (Integer)v.getTag();
|
58
|
+
String title = pageViewModel.getTabTitle(tabIndex);
|
59
|
+
TabTitleChangeDialogFragment dialog = TabTitleChangeDialogFragment.newInstance(tabIndex, title);
|
60
|
+
dialog.show(getSupportFragmentManager(), "TabTitleChangeDialogFragment");
|
61
|
+
return true;
|
62
|
+
}
|
63
|
+
|
64
|
+
@Override
|
65
|
+
public void onTabTitleChange(TabTitleChangeDialogFragment dialog) {
|
66
|
+
String title = dialog.getTitle();
|
67
|
+
int tabIndex = dialog.getTabIndex();
|
68
|
+
tabLayout.getTabAt(tabIndex).setText(title); //ここでタブの名前を変更
|
69
|
+
|
70
|
+
pageViewModel.setTabTitle(tabIndex, title);
|
71
|
+
pageViewModel.saveTabTitles(sharedPreferences);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
```
|
75
|
+
res/layout/activity_main.xml
|
76
|
+
```xml
|
77
|
+
<?xml version="1.0" encoding="utf-8"?>
|
78
|
+
<androidx.coordinatorlayout.widget.CoordinatorLayout
|
79
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
80
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
81
|
+
xmlns:tools="http://schemas.android.com/tools"
|
82
|
+
android:layout_width="match_parent"
|
83
|
+
android:layout_height="match_parent"
|
84
|
+
tools:context=".MainActivity">
|
85
|
+
|
86
|
+
<com.google.android.material.appbar.AppBarLayout
|
87
|
+
android:layout_width="match_parent"
|
88
|
+
android:layout_height="wrap_content"
|
89
|
+
android:theme="@style/Theme.Q375388.AppBarOverlay">
|
90
|
+
|
91
|
+
<TextView
|
92
|
+
android:id="@+id/title"
|
93
|
+
android:layout_width="wrap_content"
|
94
|
+
android:layout_height="wrap_content"
|
95
|
+
android:gravity="center"
|
96
|
+
android:minHeight="?actionBarSize"
|
97
|
+
android:padding="@dimen/appbar_padding"
|
98
|
+
android:text="@string/app_name"
|
99
|
+
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
|
100
|
+
|
101
|
+
<com.google.android.material.tabs.TabLayout
|
102
|
+
android:id="@+id/tablayout"
|
103
|
+
android:layout_width="match_parent"
|
104
|
+
android:layout_height="wrap_content" />
|
105
|
+
|
106
|
+
</com.google.android.material.appbar.AppBarLayout>
|
107
|
+
|
108
|
+
<androidx.viewpager2.widget.ViewPager2
|
109
|
+
android:id="@+id/pager"
|
110
|
+
android:layout_width="match_parent"
|
111
|
+
android:layout_height="match_parent" />
|
112
|
+
|
113
|
+
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
114
|
+
```
|
115
|
+
ui.main.PageViewModel.java
|
116
|
+
```java
|
117
|
+
package com.teratail.q375388.ui.main;
|
118
|
+
|
119
|
+
import android.content.SharedPreferences;
|
120
|
+
|
121
|
+
import androidx.lifecycle.ViewModel;
|
122
|
+
|
123
|
+
public class PageViewModel extends ViewModel {
|
124
|
+
private String[] tabTitles;
|
125
|
+
|
126
|
+
public String getTabTitle(int index) { return tabTitles[index]; }
|
127
|
+
public void setTabTitle(int index, String title) { tabTitles[index] = title; }
|
128
|
+
|
129
|
+
public void restoreTabTitles(SharedPreferences sharedPreferences, String[] defaultTitles) {
|
130
|
+
tabTitles = new String[defaultTitles.length];
|
131
|
+
|
132
|
+
if(sharedPreferences == null) {
|
133
|
+
for(int i=0; i<tabTitles.length; i++) {
|
134
|
+
tabTitles[i] = defaultTitles[i];
|
135
|
+
}
|
136
|
+
} else {
|
137
|
+
for(int i=0; i<tabTitles.length; i++) {
|
138
|
+
tabTitles[i] = sharedPreferences.getString(getPreferencesKey(i), defaultTitles[i]);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
}
|
142
|
+
public void saveTabTitles(SharedPreferences sharedPreferences) {
|
143
|
+
SharedPreferences.Editor editor = sharedPreferences.edit();
|
144
|
+
editor.clear();
|
145
|
+
for(int i=0; i<tabTitles.length; i++) {
|
146
|
+
editor.putString(getPreferencesKey(i), tabTitles[i]);
|
147
|
+
}
|
148
|
+
editor.commit();
|
149
|
+
}
|
150
|
+
|
151
|
+
private String getPreferencesKey(int i) { return "tab"+(i+1); }
|
152
|
+
}
|
153
|
+
```
|
154
|
+
ui.main.TabTitleChangeDialogFragment.java
|
155
|
+
```java
|
156
|
+
package com.teratail.q375388.ui.main;
|
157
|
+
|
158
|
+
import android.app.Dialog;
|
159
|
+
import android.content.*;
|
160
|
+
import android.os.Bundle;
|
161
|
+
import android.view.*;
|
162
|
+
import android.widget.EditText;
|
163
|
+
|
164
|
+
import androidx.annotation.Nullable;
|
165
|
+
import androidx.appcompat.app.AlertDialog;
|
166
|
+
import androidx.fragment.app.*;
|
167
|
+
|
168
|
+
import com.teratail.q375388.R;
|
169
|
+
|
170
|
+
public class TabTitleChangeDialogFragment extends DialogFragment {
|
171
|
+
public interface Listener {
|
172
|
+
void onTabTitleChange(TabTitleChangeDialogFragment dialog);
|
173
|
+
}
|
174
|
+
|
175
|
+
private static final String ARGS_TAB_INDEX = "TabIndex";
|
176
|
+
private static final String ARGS_TITLE = "Title";
|
177
|
+
|
178
|
+
public static TabTitleChangeDialogFragment newInstance(int tabIndex, String title) {
|
179
|
+
TabTitleChangeDialogFragment dialog = new TabTitleChangeDialogFragment();
|
180
|
+
Bundle args = new Bundle();
|
181
|
+
args.putInt(ARGS_TAB_INDEX, tabIndex);
|
182
|
+
args.putString(ARGS_TITLE, title);
|
183
|
+
dialog.setArguments(args);
|
184
|
+
return dialog;
|
185
|
+
}
|
186
|
+
|
187
|
+
private int tabIndex;
|
188
|
+
private String title;
|
189
|
+
private Listener listener;
|
190
|
+
private EditText editText;
|
191
|
+
|
192
|
+
@Override
|
193
|
+
public void onCreate(@Nullable Bundle savedInstanceState) {
|
194
|
+
super.onCreate(savedInstanceState);
|
195
|
+
Bundle args = getArguments();
|
196
|
+
tabIndex = args.getInt(ARGS_TAB_INDEX);
|
197
|
+
title = args.getString(ARGS_TITLE);
|
198
|
+
}
|
199
|
+
|
200
|
+
public int getTabIndex() { return tabIndex; }
|
201
|
+
public String getTitle() { return editText.getText().toString(); }
|
202
|
+
|
203
|
+
@Override
|
204
|
+
public void onAttach(Context context) {
|
205
|
+
super.onAttach(context);
|
206
|
+
try {
|
207
|
+
listener = (Listener)context;
|
208
|
+
} catch (ClassCastException e) {
|
209
|
+
throw new ClassCastException(context.toString() + " must implement TabTitleChangeDialogListener");
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
@Override
|
214
|
+
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
215
|
+
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
216
|
+
LayoutInflater inflater = requireActivity().getLayoutInflater();
|
217
|
+
View inputView = inflater.inflate(R.layout.fragment_tab_title_change_dialog, null);
|
218
|
+
editText = inputView.findViewById(R.id.editText1);
|
219
|
+
editText.setText(title);
|
220
|
+
editText.requestFocus();
|
221
|
+
editText.setSelection(0, title.length());
|
222
|
+
|
223
|
+
builder.setView(inputView)
|
224
|
+
.setTitle("カテゴリ名変更")
|
225
|
+
.setPositiveButton("変更", new DialogInterface.OnClickListener() {
|
226
|
+
public void onClick(DialogInterface dialog, int id) {
|
227
|
+
listener.onTabTitleChange(TabTitleChangeDialogFragment.this);
|
228
|
+
}
|
229
|
+
})
|
230
|
+
.setNegativeButton("キャンセル", (dialog, id) -> TabTitleChangeDialogFragment.this.getDialog().cancel());
|
231
|
+
return builder.create();
|
232
|
+
}
|
233
|
+
}
|
234
|
+
```
|
235
|
+
res/layout/fragment_tab_title_change_dialog.xml
|
236
|
+
```xml
|
237
|
+
<?xml version="1.0" encoding="utf-8"?>
|
238
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
239
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
240
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
241
|
+
xmlns:tools="http://schemas.android.com/tools"
|
242
|
+
android:layout_width="match_parent"
|
243
|
+
android:layout_height="match_parent"
|
244
|
+
tools:context=".ui.main.TabTitleChangeDialogFragment">
|
245
|
+
|
246
|
+
<EditText
|
247
|
+
android:id="@+id/editText1"
|
248
|
+
android:layout_width="wrap_content"
|
249
|
+
android:layout_height="wrap_content"
|
250
|
+
android:ems="16"
|
251
|
+
android:singleLine="true"
|
252
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
253
|
+
app:layout_constraintTop_toTopOf="parent" />
|
254
|
+
|
255
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
256
|
+
```
|