回答編集履歴
5
setCompoundDrawablesRelativeWithIntrinsicBounds をリソース ID 版へ
test
CHANGED
@@ -10,7 +10,6 @@
|
|
10
10
|
import androidx.fragment.app.Fragment;
|
11
11
|
|
12
12
|
import android.content.*;
|
13
|
-
import android.graphics.drawable.Drawable;
|
14
13
|
import android.os.Bundle;
|
15
14
|
import android.view.*;
|
16
15
|
import android.widget.*;
|
@@ -128,8 +127,7 @@
|
|
128
127
|
Currency currency = Currency.values()[pref.getInt(PREF_CURRENCY_VALUE, 0)];
|
129
128
|
|
130
129
|
EditText price = view.findViewById(R.id.enter_price);
|
131
|
-
Drawable currencyIcon = getResources().getDrawable(currency.drawableResId, null);
|
132
|
-
price.setCompoundDrawablesRelativeWithIntrinsicBounds(currencyI
|
130
|
+
price.setCompoundDrawablesRelativeWithIntrinsicBounds(currency.drawableResId, 0, 0, 0);
|
133
131
|
}
|
134
132
|
}
|
135
133
|
}
|
4
アイコンのリソース名等を変更
test
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
---
|
4
4
|
|
5
5
|
フラグメントでセッティング画面とメイン画面を作ってやってみました。
|
6
|
-
drawable に ic_
|
6
|
+
drawable に ic_money, ic_yen, ic_euro という各アイコンがある前提です。
|
7
7
|
```java
|
8
8
|
import androidx.annotation.*;
|
9
9
|
import androidx.appcompat.app.AppCompatActivity;
|
@@ -40,9 +40,9 @@
|
|
40
40
|
}
|
41
41
|
|
42
42
|
enum Currency {
|
43
|
-
U
|
43
|
+
USD("US Dollers", R.drawable.ic_money),
|
44
|
-
J
|
44
|
+
JPY("Japanese Yen", R.drawable.ic_yen),
|
45
|
-
E
|
45
|
+
EURO("Euro", R.drawable.ic_euro);
|
46
46
|
|
47
47
|
final String text;
|
48
48
|
final int drawableResId;
|
3
enum Currency の valueOf(int) メソッド廃止
test
CHANGED
@@ -44,10 +44,6 @@
|
|
44
44
|
JapaneseYen("Japanese Yen", R.drawable.ic_japanese_yen),
|
45
45
|
Euro("Euro", R.drawable.ic_euro);
|
46
46
|
|
47
|
-
static Currency valueOf(int ordinal) {
|
48
|
-
return values()[ordinal];
|
49
|
-
}
|
50
|
-
|
51
47
|
final String text;
|
52
48
|
final int drawableResId;
|
53
49
|
Currency(String text, int drawableResId) {
|
@@ -65,7 +61,7 @@
|
|
65
61
|
super.onViewCreated(view, savedInstanceState);
|
66
62
|
|
67
63
|
SharedPreferences pref = requireActivity().getSharedPreferences(PREF_CURRENCY_FILENAME, Context.MODE_PRIVATE);
|
68
|
-
Currency currency = Currency.value
|
64
|
+
Currency currency = Currency.values()[pref.getInt(PREF_CURRENCY_VALUE, 0)];
|
69
65
|
|
70
66
|
Spinner spinner = view.findViewById(R.id.currency_spinner);
|
71
67
|
spinner.setAdapter(new CurrencySpinnerAdapter());
|
@@ -129,7 +125,7 @@
|
|
129
125
|
super.onViewCreated(view, savedInstanceState);
|
130
126
|
|
131
127
|
SharedPreferences pref = requireActivity().getSharedPreferences(PREF_CURRENCY_FILENAME, Context.MODE_PRIVATE);
|
132
|
-
Currency currency = Currency.value
|
128
|
+
Currency currency = Currency.values()[pref.getInt(PREF_CURRENCY_VALUE, 0)];
|
133
129
|
|
134
130
|
EditText price = view.findViewById(R.id.enter_price);
|
135
131
|
Drawable currencyIcon = getResources().getDrawable(currency.drawableResId, null);
|
2
セッティングの選択をラジオボタンからスピナーへ
test
CHANGED
@@ -7,17 +7,16 @@
|
|
7
7
|
```java
|
8
8
|
import androidx.annotation.*;
|
9
9
|
import androidx.appcompat.app.AppCompatActivity;
|
10
|
-
import androidx.core.content.res.ResourcesCompat;
|
11
10
|
import androidx.fragment.app.Fragment;
|
12
11
|
|
13
12
|
import android.content.*;
|
14
13
|
import android.graphics.drawable.Drawable;
|
15
14
|
import android.os.Bundle;
|
16
|
-
import android.view.
|
15
|
+
import android.view.*;
|
17
16
|
import android.widget.*;
|
18
17
|
|
19
18
|
public class MainActivity extends AppCompatActivity {
|
20
|
-
private static final String PREF_CURRENCY_FILENAME = "
|
19
|
+
private static final String PREF_CURRENCY_FILENAME = "CURRENCY_SHARED";
|
21
20
|
private static final String PREF_CURRENCY_VALUE = "currencyValue";
|
22
21
|
|
23
22
|
@Override
|
@@ -41,23 +40,19 @@
|
|
41
40
|
}
|
42
41
|
|
43
42
|
enum Currency {
|
44
|
-
JapaneseYen(R.id.yen, R.drawable.ic_japanese_yen),
|
45
|
-
UsDoller(
|
43
|
+
UsDoller("Us Dollers", R.drawable.ic_us_doller),
|
44
|
+
JapaneseYen("Japanese Yen", R.drawable.ic_japanese_yen),
|
46
|
-
Euro(
|
45
|
+
Euro("Euro", R.drawable.ic_euro);
|
47
46
|
|
48
47
|
static Currency valueOf(int ordinal) {
|
49
48
|
return values()[ordinal];
|
50
49
|
}
|
51
|
-
|
50
|
+
|
52
|
-
for(Currency c : values()) if(c.layoutId == id) return c;
|
53
|
-
throw new IllegalArgumentException("id=" + id);
|
54
|
-
}
|
55
|
-
|
56
|
-
|
51
|
+
final String text;
|
57
52
|
final int drawableResId;
|
58
|
-
Currency(in
|
53
|
+
Currency(String text, int drawableResId) {
|
59
|
-
this.
|
54
|
+
this.text = text;
|
60
|
-
this.
|
55
|
+
this.drawableResId = drawableResId;
|
61
56
|
}
|
62
57
|
}
|
63
58
|
|
@@ -72,13 +67,56 @@
|
|
72
67
|
SharedPreferences pref = requireActivity().getSharedPreferences(PREF_CURRENCY_FILENAME, Context.MODE_PRIVATE);
|
73
68
|
Currency currency = Currency.valueOf(pref.getInt(PREF_CURRENCY_VALUE, 0));
|
74
69
|
|
75
|
-
|
70
|
+
Spinner spinner = view.findViewById(R.id.currency_spinner);
|
76
|
-
|
71
|
+
spinner.setAdapter(new CurrencySpinnerAdapter());
|
77
|
-
|
72
|
+
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
73
|
+
@Override
|
74
|
+
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
|
78
|
-
SharedPreferences.Editor editor = pref.edit();
|
75
|
+
SharedPreferences.Editor editor = pref.edit();
|
79
|
-
editor.putInt(PREF_CURRENCY_VALUE,
|
76
|
+
editor.putInt(PREF_CURRENCY_VALUE, position);
|
80
|
-
editor.commit();
|
77
|
+
editor.commit();
|
78
|
+
}
|
79
|
+
@Override
|
80
|
+
public void onNothingSelected(AdapterView<?> adapterView) {
|
81
|
+
}
|
81
82
|
});
|
83
|
+
spinner.setSelection(currency.ordinal());
|
84
|
+
}
|
85
|
+
|
86
|
+
class CurrencySpinnerAdapter extends BaseAdapter {
|
87
|
+
@Override
|
88
|
+
public int getCount() {
|
89
|
+
return Currency.values().length;
|
90
|
+
}
|
91
|
+
|
92
|
+
@Override
|
93
|
+
public Object getItem(int i) {
|
94
|
+
return Currency.values()[i];
|
95
|
+
}
|
96
|
+
|
97
|
+
@Override
|
98
|
+
public long getItemId(int i) {
|
99
|
+
return 0;
|
100
|
+
}
|
101
|
+
|
102
|
+
@NonNull
|
103
|
+
@Override
|
104
|
+
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
105
|
+
if(convertView == null) {
|
106
|
+
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.currency_spinner, parent, false);
|
107
|
+
}
|
108
|
+
TextView nameView = convertView.findViewById(R.id.currency_name_textview);
|
109
|
+
ImageView symbolView = convertView.findViewById(R.id.currency_symbol);
|
110
|
+
Currency currency = Currency.values()[position];
|
111
|
+
nameView.setText(currency.text);
|
112
|
+
symbolView.setImageResource(currency.drawableResId);
|
113
|
+
return convertView;
|
114
|
+
}
|
115
|
+
|
116
|
+
@Override
|
117
|
+
public View getDropDownView(int position, View convertView, ViewGroup parent) {
|
118
|
+
return getView(position, convertView, parent);
|
119
|
+
}
|
82
120
|
}
|
83
121
|
}
|
84
122
|
|
@@ -177,28 +215,37 @@
|
|
177
215
|
android:layout_height="match_parent"
|
178
216
|
tools:context=".MainActivity">
|
179
217
|
|
180
|
-
<
|
218
|
+
<Spinner
|
181
|
-
android:id="@+id/currency"
|
219
|
+
android:id="@+id/currency_spinner"
|
182
|
-
android:layout_width="
|
220
|
+
android:layout_width="0dp"
|
183
|
-
android:layout_height="wrap_content"
|
221
|
+
android:layout_height="wrap_content"
|
184
|
-
app:layout_constraintEnd_toEndOf="parent"
|
222
|
+
app:layout_constraintEnd_toEndOf="parent"
|
185
|
-
app:layout_constraintStart_toStartOf="parent"
|
223
|
+
app:layout_constraintStart_toStartOf="parent"
|
186
|
-
app:layout_constraintTop_toTopOf="parent" >
|
224
|
+
app:layout_constraintTop_toTopOf="parent" />
|
187
|
-
<RadioButton
|
188
|
-
android:id="@+id/yen"
|
189
|
-
android:text="円"
|
190
|
-
android:layout_width="wrap_content"
|
191
|
-
android:layout_height="wrap_content" />
|
192
|
-
<RadioButton
|
193
|
-
android:id="@+id/doller"
|
194
|
-
android:text="ドル"
|
195
|
-
android:layout_width="wrap_content"
|
196
|
-
android:layout_height="wrap_content" />
|
197
|
-
<RadioButton
|
198
|
-
android:id="@+id/euro"
|
199
|
-
android:text="ユーロ"
|
200
|
-
android:layout_width="wrap_content"
|
201
|
-
android:layout_height="wrap_content" />
|
202
|
-
</RadioGroup>
|
203
|
-
</androidx.constraintlayout.widget.ConstraintLayout>
|
225
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
204
|
-
```
|
226
|
+
```
|
227
|
+
res/layout/currency_spinner.xml
|
228
|
+
```xml
|
229
|
+
<?xml version="1.0" encoding="utf-8"?>
|
230
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
231
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
232
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
233
|
+
android:layout_width="match_parent"
|
234
|
+
android:layout_height="wrap_content">
|
235
|
+
<ImageView
|
236
|
+
android:id="@+id/currency_symbol"
|
237
|
+
android:layout_width="32dp"
|
238
|
+
android:layout_height="32dp"
|
239
|
+
app:layout_constraintStart_toStartOf="parent"
|
240
|
+
app:layout_constraintTop_toTopOf="parent" />
|
241
|
+
<TextView
|
242
|
+
android:id="@+id/currency_name_textview"
|
243
|
+
android:layout_width="0dp"
|
244
|
+
android:layout_height="wrap_content"
|
245
|
+
android:text="currency_name"
|
246
|
+
app:layout_constraintBottom_toBottomOf="@id/currency_symbol"
|
247
|
+
app:layout_constraintEnd_toEndOf="parent"
|
248
|
+
app:layout_constraintStart_toEndOf="@id/currency_symbol"
|
249
|
+
app:layout_constraintTop_toTopOf="@id/currency_symbol" />
|
250
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
251
|
+
```
|
1
コード追加
test
CHANGED
@@ -1 +1,204 @@
|
|
1
1
|
SharedPreferences を用いるのは如何でしょうか。
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
フラグメントでセッティング画面とメイン画面を作ってやってみました。
|
6
|
+
drawable に ic_japanese_yen, ic_us_dollers, ic_euro という各アイコンがある前提です。
|
7
|
+
```java
|
8
|
+
import androidx.annotation.*;
|
9
|
+
import androidx.appcompat.app.AppCompatActivity;
|
10
|
+
import androidx.core.content.res.ResourcesCompat;
|
11
|
+
import androidx.fragment.app.Fragment;
|
12
|
+
|
13
|
+
import android.content.*;
|
14
|
+
import android.graphics.drawable.Drawable;
|
15
|
+
import android.os.Bundle;
|
16
|
+
import android.view.View;
|
17
|
+
import android.widget.*;
|
18
|
+
|
19
|
+
public class MainActivity extends AppCompatActivity {
|
20
|
+
private static final String PREF_CURRENCY_FILENAME = "currency";
|
21
|
+
private static final String PREF_CURRENCY_VALUE = "currencyValue";
|
22
|
+
|
23
|
+
@Override
|
24
|
+
protected void onCreate(Bundle savedInstanceState) {
|
25
|
+
super.onCreate(savedInstanceState);
|
26
|
+
setContentView(R.layout.activity_main);
|
27
|
+
|
28
|
+
Button settingButton = findViewById(R.id.settingButton);
|
29
|
+
settingButton.setOnClickListener(v -> {
|
30
|
+
getSupportFragmentManager().beginTransaction()
|
31
|
+
.replace(R.id.fragment_container, new SettingFragment())
|
32
|
+
.commit();
|
33
|
+
});
|
34
|
+
|
35
|
+
Button mainButton = findViewById(R.id.mainButton);
|
36
|
+
mainButton.setOnClickListener(v -> {
|
37
|
+
getSupportFragmentManager().beginTransaction()
|
38
|
+
.replace(R.id.fragment_container, new MainFragment())
|
39
|
+
.commit();
|
40
|
+
});
|
41
|
+
}
|
42
|
+
|
43
|
+
enum Currency {
|
44
|
+
JapaneseYen(R.id.yen, R.drawable.ic_japanese_yen),
|
45
|
+
UsDoller(R.id.doller, R.drawable.ic_us_doller),
|
46
|
+
Euro(R.id.euro, R.drawable.ic_euro);
|
47
|
+
|
48
|
+
static Currency valueOf(int ordinal) {
|
49
|
+
return values()[ordinal];
|
50
|
+
}
|
51
|
+
static Currency fromLayoutId(int id) {
|
52
|
+
for(Currency c : values()) if(c.layoutId == id) return c;
|
53
|
+
throw new IllegalArgumentException("id=" + id);
|
54
|
+
}
|
55
|
+
|
56
|
+
private final int layoutId;
|
57
|
+
final int drawableResId;
|
58
|
+
Currency(int layoutId, int drawableResId) {
|
59
|
+
this.layoutId = layoutId;
|
60
|
+
this. drawableResId = drawableResId;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
public static class SettingFragment extends Fragment {
|
65
|
+
SettingFragment() {
|
66
|
+
super(R.layout.fragment_setting);
|
67
|
+
}
|
68
|
+
@Override
|
69
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
70
|
+
super.onViewCreated(view, savedInstanceState);
|
71
|
+
|
72
|
+
SharedPreferences pref = requireActivity().getSharedPreferences(PREF_CURRENCY_FILENAME, Context.MODE_PRIVATE);
|
73
|
+
Currency currency = Currency.valueOf(pref.getInt(PREF_CURRENCY_VALUE, 0));
|
74
|
+
|
75
|
+
RadioGroup currencyGroup = view.findViewById(R.id.currency);
|
76
|
+
currencyGroup.check(currency.layoutId);
|
77
|
+
currencyGroup.setOnCheckedChangeListener((radioGroup, checkedId) -> {
|
78
|
+
SharedPreferences.Editor editor = pref.edit();
|
79
|
+
editor.putInt(PREF_CURRENCY_VALUE, Currency.fromLayoutId(checkedId).ordinal());
|
80
|
+
editor.commit();
|
81
|
+
});
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
public static class MainFragment extends Fragment {
|
86
|
+
MainFragment() {
|
87
|
+
super(R.layout.fragment_main);
|
88
|
+
}
|
89
|
+
@Override
|
90
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
91
|
+
super.onViewCreated(view, savedInstanceState);
|
92
|
+
|
93
|
+
SharedPreferences pref = requireActivity().getSharedPreferences(PREF_CURRENCY_FILENAME, Context.MODE_PRIVATE);
|
94
|
+
Currency currency = Currency.valueOf(pref.getInt(PREF_CURRENCY_VALUE, 0));
|
95
|
+
|
96
|
+
EditText price = view.findViewById(R.id.enter_price);
|
97
|
+
Drawable currencyIcon = getResources().getDrawable(currency.drawableResId, null);
|
98
|
+
price.setCompoundDrawablesRelativeWithIntrinsicBounds(currencyIcon, null, null, null);
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
```
|
103
|
+
res/layout/activity_main.xml
|
104
|
+
```xml
|
105
|
+
<?xml version="1.0" encoding="utf-8"?>
|
106
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
107
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
108
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
109
|
+
xmlns:tools="http://schemas.android.com/tools"
|
110
|
+
android:layout_width="match_parent"
|
111
|
+
android:layout_height="match_parent"
|
112
|
+
tools:context=".MainActivity">
|
113
|
+
<Button
|
114
|
+
android:id="@+id/settingButton"
|
115
|
+
android:text="SETTING"
|
116
|
+
android:layout_width="0dp"
|
117
|
+
android:layout_height="wrap_content"
|
118
|
+
app:layout_constraintEnd_toStartOf="@id/mainButton"
|
119
|
+
app:layout_constraintStart_toStartOf="parent"
|
120
|
+
app:layout_constraintTop_toTopOf="parent" />
|
121
|
+
<Button
|
122
|
+
android:id="@+id/mainButton"
|
123
|
+
android:text="MAIN"
|
124
|
+
android:layout_width="0dp"
|
125
|
+
android:layout_height="wrap_content"
|
126
|
+
app:layout_constraintEnd_toEndOf="parent"
|
127
|
+
app:layout_constraintStart_toEndOf="@id/settingButton"
|
128
|
+
app:layout_constraintTop_toTopOf="parent" />
|
129
|
+
<FrameLayout
|
130
|
+
android:id="@+id/fragment_container"
|
131
|
+
android:layout_width="0dp"
|
132
|
+
android:layout_height="0dp"
|
133
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
134
|
+
app:layout_constraintEnd_toEndOf="parent"
|
135
|
+
app:layout_constraintStart_toStartOf="parent"
|
136
|
+
app:layout_constraintTop_toBottomOf="@id/settingButton" />
|
137
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
138
|
+
```
|
139
|
+
res/layout/fragment_main.xml
|
140
|
+
```xml
|
141
|
+
<?xml version="1.0" encoding="utf-8"?>
|
142
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
143
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
144
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
145
|
+
xmlns:tools="http://schemas.android.com/tools"
|
146
|
+
android:layout_width="match_parent"
|
147
|
+
android:layout_height="match_parent"
|
148
|
+
tools:context=".MainActivity">
|
149
|
+
|
150
|
+
<EditText
|
151
|
+
android:id="@+id/enter_price"
|
152
|
+
android:layout_width="wrap_content"
|
153
|
+
android:layout_height="wrap_content"
|
154
|
+
android:layout_marginTop="10dp"
|
155
|
+
android:ems="10"
|
156
|
+
android:hint="new_budget_tracker_price_hint"
|
157
|
+
android:inputType="number"
|
158
|
+
app:layout_constraintEnd_toEndOf="parent"
|
159
|
+
app:layout_constraintStart_toStartOf="parent"
|
160
|
+
app:layout_constraintTop_toTopOf="parent"
|
161
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
162
|
+
android:drawableStart="@drawable/ic_japanese_yen"
|
163
|
+
android:paddingEnd="5dp"
|
164
|
+
android:drawablePadding="12dp"
|
165
|
+
tools:ignore="SpeakableTextPresentCheck" />
|
166
|
+
|
167
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
168
|
+
```
|
169
|
+
res/layout/fragment_setting.xml
|
170
|
+
```xml
|
171
|
+
<?xml version="1.0" encoding="utf-8"?>
|
172
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
173
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
174
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
175
|
+
xmlns:tools="http://schemas.android.com/tools"
|
176
|
+
android:layout_width="match_parent"
|
177
|
+
android:layout_height="match_parent"
|
178
|
+
tools:context=".MainActivity">
|
179
|
+
|
180
|
+
<RadioGroup
|
181
|
+
android:id="@+id/currency"
|
182
|
+
android:layout_width="wrap_content"
|
183
|
+
android:layout_height="wrap_content"
|
184
|
+
app:layout_constraintEnd_toEndOf="parent"
|
185
|
+
app:layout_constraintStart_toStartOf="parent"
|
186
|
+
app:layout_constraintTop_toTopOf="parent" >
|
187
|
+
<RadioButton
|
188
|
+
android:id="@+id/yen"
|
189
|
+
android:text="円"
|
190
|
+
android:layout_width="wrap_content"
|
191
|
+
android:layout_height="wrap_content" />
|
192
|
+
<RadioButton
|
193
|
+
android:id="@+id/doller"
|
194
|
+
android:text="ドル"
|
195
|
+
android:layout_width="wrap_content"
|
196
|
+
android:layout_height="wrap_content" />
|
197
|
+
<RadioButton
|
198
|
+
android:id="@+id/euro"
|
199
|
+
android:text="ユーロ"
|
200
|
+
android:layout_width="wrap_content"
|
201
|
+
android:layout_height="wrap_content" />
|
202
|
+
</RadioGroup>
|
203
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
204
|
+
```
|