実現したいこと
Android Studioを使ってAndroidアプリを作成しています。
このプログラムで、下記動作を実現したいです。
・frag1でボタンを押す(textView:frag1_World)
・frag2に切り替える
・再度frag1に切り替えたとき、frag1_Worldが表示された状態にする。(画面保持)
MainActivity.javaimport
1import android.os.Bundle; 2import android.support.annotation.RequiresApi; 3import android.support.v4.app.FragmentManager; 4import android.support.v4.app.FragmentTabHost; 5import android.support.v7.app.AppCompatActivity; 6import android.util.Log; 7import android.widget.TabHost.TabSpec; 8 9 10public class MainActivity extends AppCompatActivity { 11 12 private FragmentTabHost mTabHost; 13 private String[] TabTag = { "Dice", "Character"}; 14 15 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 if (savedInstanceState == null) { 22 23 //FragmentManagerの取得 24 FragmentManager mFragmentManager = getSupportFragmentManager(); 25 26 // FragmentTabHostを取得する 27 mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 28 29 // Context、FragmentManager、タブ選択時にFragmentを挿入するリソースID 30 mTabHost.setup(this, getSupportFragmentManager(), R.id.content); 31 32 //String型の引数には任意のidを渡す 33 //今回は2つのFragmentをFragmentTabHostから切り替えるため、2つのTabSpecを用意する 34 TabSpec mTabSpec1 = mTabHost.newTabSpec("tab1"); 35 TabSpec mTabSpec2 = mTabHost.newTabSpec("tab2"); 36 37 //Tab上に表示する文字を渡す 38 mTabSpec1.setIndicator("frag1"); 39 mTabSpec2.setIndicator("frag2"); 40 41 Bundle args = new Bundle(); 42 args.putString("string", "message"); 43 44 45 mTabHost.addTab(mTabSpec1, Fragment1.class, args); 46 mTabHost.addTab(mTabSpec2, Fragment2.class, args); 47 Log.d("debug","add"); 48 49 } 50 } 51} 52
activity_main.xml<!
1<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@android:id/tabhost" 3 xmlns:ads="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@color/background" 7 > 8 <!-- --> 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical" > 13 <!-- タブパネル操作のためのTabWidgetを配置 --> 14 <TabWidget 15 android:id="@android:id/tabs" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_weight="0" 19 android:orientation="horizontal" /> 20 <!-- タブパネルを束ねるためのコンテナーを配置 --> 21 <FrameLayout 22 android:id="@android:id/tabcontent" 23 android:layout_width="0dp" 24 android:layout_height="0dp" 25 android:layout_weight="0" /> 26 <!-- タブが押された時に表示するコンテンツ領域(Fragment) --> 27 <FrameLayout 28 android:id="@+id/content" 29 android:layout_width="match_parent" 30 android:layout_height="0dp" 31 android:layout_weight="1" 32 android:layout_marginTop="20dp" 33 android:layout_marginBottom="20dp" 34 android:layout_marginLeft="10dp" 35 android:layout_marginRight="10dp" 36 /> 37 </LinearLayout> 38</android.support.v4.app.FragmentTabHost>
Fragment1.java
1import android.support.annotation.Nullable; 2import android.support.v4.app.Fragment; 3import android.os.Bundle; 4import android.util.Log; 5import android.view.LayoutInflater; 6 7 8public class Fragment1 extends Fragment { 9 10 TextView textView1; 11 Button button1; 12 13 public void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 16 // 初期設定 -------------------------------------------------------------------------------- 17 //addTabの際にBundleを渡す場合は、Bundleから値を取得する 18 //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する 19 Bundle args = getArguments(); 20 String str = args.getString("string"); 21 //------------------------------------------------------------------------------- 22 } 23 24 @Nullable 25 @Override 26 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 27 final View v = inflater.inflate(R.layout.frag1, null); 28 29 30 Log.d("debug", "frag1_OnCreateView"); 31 32 // text ------------------------------------------------------------------------------------ 33 textView1 = v.findViewById(R.id.text_view1); 34 textView1.setText("Frag1_Hello"); 35 36 button1 = v.findViewById(R.id.button1); 37 button1.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 textView1.setText("frag1_World"); 41 } 42 }); 43 44 45 return v; 46 } 47 48 @Override 49 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 50 super.onViewCreated(view, savedInstanceState); 51 Log.d("test", "createFragment1"); 52 } 53 54 @Override 55 public void onSaveInstanceState(Bundle outState) { 56 String i = "Hi"; 57 outState.putString("editText", i); 58 super.onSaveInstanceState(outState); 59 } 60 @Override 61 public void onDestroyView() { 62 Log.d("test", "destroyFragment1"); 63 super.onDestroyView(); 64 } 65 66} 67 68
frag1.xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:id="@+id/text_view1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:textSize="30sp" 15 /> 16 17 <Button 18 android:id="@+id/button1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_margin="20dp" 22 android:text="tab1" 23 /> 24 25</LinearLayout>
Fragment2.java
1import android.support.annotation.Nullable; 2import android.support.v4.app.Fragment; 3import android.os.Bundle; 4import android.util.Log; 5import android.view.LayoutInflater; 6 7/* 8 * タブがクリックされた時に表示するFragment 9 */ 10public class Fragment2 extends Fragment { 11 12 TextView textView2; 13 Button button2; 14 15 16 @Override 17 public void onCreate(Bundle savedInstanceState){ 18 super.onCreate(savedInstanceState); 19 20 //addTabの際にBundleを渡す場合は、Bundleから値を取得する 21 //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する 22 Bundle args = getArguments(); 23 String str = args.getString("string"); 24 } 25 26 @Nullable 27 @Override 28 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 29 View v = inflater.inflate(R.layout.frag2, null); 30 31 Log.d("debug", "frag2_OnCreateView"); 32 33 // text ------------------------------------------------------------------------------------ 34 textView2 = v.findViewById(R.id.text_view2); 35 textView2.setText("frag2_Hello"); 36 37 button2 = v.findViewById(R.id.button2); 38 button2.setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View v) { 41 textView2.setText("frag2_World"); 42 } 43 }); 44 return v; 45 } 46 @Override 47 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 48 super.onViewCreated(view, savedInstanceState); 49 Log.d("test", "createFragment2"); 50 } 51 52 @Override 53 public void onSaveInstanceState(Bundle outState) { 54 String i = "Hi"; 55 outState.putString("editText", i); 56 super.onSaveInstanceState(outState); 57 } 58 @Override 59 public void onDestroyView() { 60 Log.d("test", "destroyFragment2"); 61 super.onDestroyView(); 62 } 63}
frag2.xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 > 7 <TextView 8 android:id="@+id/text_view2" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="30sp" 12 /> 13 14 <Button 15 android:id="@+id/button2" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_margin="20dp" 19 android:text="tab2" 20 /> 21 22</LinearLayout>
回答1件
あなたの回答
tips
プレビュー