質問編集履歴
1
コードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,13 +1,123 @@
|
|
1
1
|
質問させていただきます。
|
2
2
|
|
3
|
+
MainActivityクラスのコード
|
4
|
+
```Java
|
3
|
-
|
5
|
+
public class MainActivity extends AppCompatActivity {
|
4
6
|
|
7
|
+
@Override
|
5
|
-
|
8
|
+
protected void onCreate(Bundle savedInstanceState) {
|
9
|
+
super.onCreate(savedInstanceState);
|
10
|
+
setContentView(R.layout.activity_main);
|
6
11
|
|
12
|
+
ViewPager pager = (ViewPager) findViewById(R.id.pager);
|
13
|
+
MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
|
7
|
-
|
14
|
+
pager.setAdapter(adapter);
|
8
15
|
|
16
|
+
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
|
17
|
+
@Override
|
9
|
-
public void
|
18
|
+
public void onClick(View view) {
|
10
|
-
|
19
|
+
//ここに何を書くべきかが分かりません
|
20
|
+
}
|
21
|
+
});
|
22
|
+
}
|
11
23
|
}
|
24
|
+
```
|
25
|
+
activity_main.xmlのコード
|
26
|
+
```XML
|
27
|
+
<?xml version="1.0" encoding="utf-8"?>
|
28
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
29
|
+
android:layout_width="match_parent"
|
30
|
+
android:layout_height="match_parent"
|
31
|
+
android:orientation="vertical">
|
32
|
+
|
33
|
+
<Button
|
34
|
+
android:id="@+id/button"
|
35
|
+
android:layout_width="match_parent"
|
36
|
+
android:layout_height="wrap_content"
|
37
|
+
android:text="Button" />
|
12
38
|
|
39
|
+
<android.support.v4.view.ViewPager
|
40
|
+
android:id="@+id/pager"
|
41
|
+
android:layout_width="match_parent"
|
42
|
+
android:layout_height="wrap_content">
|
43
|
+
</android.support.v4.view.ViewPager>
|
44
|
+
</LinearLayout>
|
45
|
+
```
|
46
|
+
MyAdapterクラスのコード
|
47
|
+
```Java
|
48
|
+
public class MyAdapter extends FragmentPagerAdapter {
|
49
|
+
|
50
|
+
public MyAdapter(FragmentManager fm) {
|
51
|
+
super(fm);
|
52
|
+
}
|
53
|
+
|
54
|
+
@Override
|
55
|
+
public Fragment getItem(int i) {
|
56
|
+
switch (i){
|
57
|
+
case 0:
|
58
|
+
return new Fragment_Edit();
|
59
|
+
case 1:
|
60
|
+
return new Fragment_Edit();
|
61
|
+
default:
|
62
|
+
return new Fragment_Edit();
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
@Override
|
67
|
+
public int getCount() {
|
68
|
+
return 3;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
```
|
72
|
+
Fragment_Editクラスのコード
|
73
|
+
```Java
|
74
|
+
public class Fragment_Edit extends android.support.v4.app.Fragment {
|
75
|
+
|
76
|
+
private EditText edit;
|
77
|
+
|
78
|
+
@Nullable
|
79
|
+
@Override
|
80
|
+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
81
|
+
View v = inflater.inflate(R.layout.fragment, null);
|
82
|
+
|
83
|
+
edit = (EditText) v.findViewById(R.id.editText);
|
84
|
+
|
85
|
+
return v;
|
86
|
+
}
|
87
|
+
|
88
|
+
public void set_empty(){
|
89
|
+
edit.setText("");
|
90
|
+
}
|
91
|
+
}
|
92
|
+
```
|
93
|
+
fragment.xmlのコード
|
94
|
+
```XML
|
95
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
96
|
+
android:orientation="vertical"
|
97
|
+
android:layout_width="match_parent"
|
98
|
+
android:layout_height="match_parent">
|
99
|
+
|
100
|
+
<EditText
|
101
|
+
android:id="@+id/editText"
|
102
|
+
android:layout_width="match_parent"
|
103
|
+
android:layout_height="wrap_content"
|
104
|
+
android:text="Text" />
|
105
|
+
</LinearLayout>
|
106
|
+
```
|
107
|
+
|
13
|
-
|
108
|
+
上記のように、MainActivityのViewPagerに、同じFragmentを3つ設置しています。
|
109
|
+
|
110
|
+
このときに、MainActivityのボタンを押すことで、2ページ目のEditTextだけ文字を削除したいと考えています。
|
111
|
+
具体的には、2ページ目のFragmentのset_empty()メソッドだけを呼び出したいです。
|
112
|
+
|
113
|
+
どのようにすればよろしいでしょうか?
|
114
|
+
|
115
|
+
なお、MainActivityのオンクリックリスナーの中に
|
116
|
+
|
117
|
+
```Java
|
118
|
+
EditText editText = (EditText) findViewById(R.id.editText);
|
119
|
+
editText.setText("");
|
120
|
+
```
|
121
|
+
と記述すれば、ボタンを押した際に1ページ目のEditTextの文字は削除できましたが、それ以外のページは削除できませんでした。
|
122
|
+
|
123
|
+
どうぞよろしくお願いいたします。
|