質問編集履歴
1
情報の更新
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
[Android]ToolBarのクリックイベント
|
1
|
+
[Android]ToolBarのクリックイベントとテキストの変更
|
test
CHANGED
@@ -50,8 +50,6 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
-
|
54
|
-
|
55
53
|
itemのtextを動的に変更する方法が調べても上手く探せなかったため、
|
56
54
|
|
57
55
|
教えていただけましたら幸いです。
|
@@ -61,3 +59,129 @@
|
|
61
59
|
|
62
60
|
|
63
61
|
よろしくお願いいたします。
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
<追記>
|
66
|
+
|
67
|
+
以下の方法で、「←」「→」を押したときに日付部分を変更することが出来ました。
|
68
|
+
|
69
|
+
```menu_main.xml
|
70
|
+
|
71
|
+
<?xml version="1.0" encoding="utf-8"?>
|
72
|
+
|
73
|
+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
74
|
+
|
75
|
+
xmlns:app="http://schemas.android.com/apk/res-auto">
|
76
|
+
|
77
|
+
<item
|
78
|
+
|
79
|
+
android:id="@+id/before_day_button"
|
80
|
+
|
81
|
+
app:showAsAction="always"
|
82
|
+
|
83
|
+
android:title="←" />
|
84
|
+
|
85
|
+
<item
|
86
|
+
|
87
|
+
android:id="@+id/the_day_button"
|
88
|
+
|
89
|
+
app:showAsAction="always"
|
90
|
+
|
91
|
+
android:title="ひづけ" />
|
92
|
+
|
93
|
+
<item
|
94
|
+
|
95
|
+
android:id="@+id/next_day_button"
|
96
|
+
|
97
|
+
app:showAsAction="always"
|
98
|
+
|
99
|
+
android:title="→" />
|
100
|
+
|
101
|
+
</menu>
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
```MainActivity.java
|
106
|
+
|
107
|
+
@Override
|
108
|
+
|
109
|
+
public boolean onOptionsItemSelected(MenuItem item) {
|
110
|
+
|
111
|
+
switch (item.getItemId()) {
|
112
|
+
|
113
|
+
case R.id.before_day_button:
|
114
|
+
|
115
|
+
View aaa = toolbar.findViewById(R.id.the_day_button);
|
116
|
+
|
117
|
+
TextView tv = (TextView) aaa;
|
118
|
+
|
119
|
+
tv.setText("前日");
|
120
|
+
|
121
|
+
break;
|
122
|
+
|
123
|
+
case R.id.next_day_button:
|
124
|
+
|
125
|
+
View bbb = toolbar.findViewById(R.id.the_day_button);
|
126
|
+
|
127
|
+
TextView tva = (TextView) bbb;
|
128
|
+
|
129
|
+
tva.setText("翌日");
|
130
|
+
|
131
|
+
break;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
return true;
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
ですが、同じ処理をonCreate()上で記述すると起動エラーで落ちてしまいます。
|
144
|
+
|
145
|
+
※最初に表示する本日の日付を入れたいため、起動時に書いています。
|
146
|
+
|
147
|
+
```MainActivity.java
|
148
|
+
|
149
|
+
@Override
|
150
|
+
|
151
|
+
protected void onCreate(Bundle savedInstanceState) {
|
152
|
+
|
153
|
+
super.onCreate(savedInstanceState);
|
154
|
+
|
155
|
+
setContentView(R.layout.activity_main);
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// ツールバーをアクションバーとしてセット
|
160
|
+
|
161
|
+
toolbar = (Toolbar) findViewById(R.id.tool_bar);
|
162
|
+
|
163
|
+
toolbar.setTitle("");
|
164
|
+
|
165
|
+
// ツールバーの準備後に呼ぶ
|
166
|
+
|
167
|
+
setSupportActionBar(toolbar);
|
168
|
+
|
169
|
+
// 今日の日付を設定
|
170
|
+
|
171
|
+
View v = toolbar.findViewById(R.id.the_day_button);
|
172
|
+
|
173
|
+
TextView tv = (TextView) v;
|
174
|
+
|
175
|
+
tv.setText("今日");
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
初期表示時にどのように変更できますでしょうか。。
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
教えていただけたらと思います。
|
186
|
+
|
187
|
+
よろしくお願いいたします。
|