簡単に文章で説明するとMyFragmentActivity1からスタートして、MyFragmentActivity1のメモを追加する(Button)を押すとMyFragmentActivity2に移動して、MyFragmentActivity2のEditTextにメモを入力してするところまではできているのですが、その後のButtonを押すとEditTextのデータが、データベースはまだ使えないためButtonの下に設置したLinearLayoutに表示したいのですが、それができません。
どうすれば良いのでしょうか?
Main Activity
java
1package com.example.kanehiro.fragmentapplication; 2 3import android.support.v4.app.Fragment; 4import android.support.v4.app.FragmentActivity; 5import android.support.v4.app.FragmentTransaction; 6import android.os.Bundle; 7 8public class MainActivity extends FragmentActivity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.contents); 16 if (fragment==null){ 17 fragment=new MyFragment1(); 18 FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction(); 19 fragmentTransaction.add(R.id.contents,fragment); 20 fragmentTransaction.commit(); 21 } 22 } 23}
MyFragmentActivity2
package com.example.kanehiro.fragmentapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MyFragment2 extends Fragment implements View.OnClickListener { //とりあえずここにさらなるステップの変数 private EditText mInputMessage; private Button mSendMessage; private LinearLayout mMessageLog; private TextView mMemoMessage; public MyFragment2() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_my_fragment2,container,false); //何に使うのかわからん Button btn = (Button) view.findViewById(R.id.button); view.findViewById(R.id.send_message).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ } }); return view; } @Override public void onClick(View v) { if(v.equals(mSendMessage)) { if(mSendMessage==null||mSendMessage.length()==0){ Toast.makeText(getActivity(),"文字が入力されていません",Toast.LENGTH_SHORT).show(); }else { //新しくエラーを消すために作った場所 //XMLのViewを初期化する // ActivityからFragmentを取得 FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); Fragment fragment = fragmentManager.findFragmentById(R.id.send_message); // FragmentのViewを取得 View view = fragment.getView(); //この下のコードに Can not resolve method 'findViewById'が出ます。 mInputMessage = (EditText)view.findViewById(R.id.input_message);//ユーザーが入力するフィールド mSendMessage = (Button) view.findViewById(R.id.send_message);//SENDボタン mMessageLog = (LinearLayout)view.findViewById(R.id.message_log);//入力履歴を表示するレイアウト mMemoMessage = (TextView) view.findViewById(R.id.memo_message);//メモの履歴 //この次はデータベースや mSendMessage.setOnClickListener(this); } } } }
MyFragmentActivity1
java
1package com.example.kanehiro.fragmentapplication; 2 3import android.os.Bundle; 4import android.support.v4.app.Fragment; 5import android.support.v4.app.FragmentTransaction; 6import android.view.LayoutInflater; 7import android.view.View; 8import android.view.ViewGroup; 9 10/** 11 * A simple {@link Fragment} subclass. 12 */ 13public class MyFragment1 extends Fragment { 14 public MyFragment1() { 15 16 } 17 @Override 18 public View onCreateView(LayoutInflater inflater, ViewGroup container, 19 Bundle savedInstanceState) { 20 21 View view=inflater.inflate(R.layout.fragment_my_fragment1,container,false); 22 view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){ 23 @Override 24 public void onClick(View v){ 25 Fragment fragment=new MyFragment2(); 26 FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); 27 fragmentTransaction.replace(R.id.contents,fragment); 28 fragmentTransaction.addToBackStack(null); 29 fragmentTransaction.commit(); 30 //もともとの処理の方画面とは変えよう 31 //新しいの作ろう とりあえず押すだけのやつ メモを追加するにしよう 32 //だからButtonのTextだけでいい 33 } 34 }); 35 return view; 36 } 37}
fragment_my_fragment1
xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="center" 6 android:orientation="vertical" 7 android:background="#419932" 8 tools:context="com.example.kanehiro.fragmentapplication.MyFragment1"> 9 10 <Button 11 android:id="@+id/button" 12 android:layout_width="211dp" 13 android:layout_height="wrap_content" 14 android:layout_gravity="center_horizontal" 15 android:text="メモを追加する"/> 16 17</LinearLayout>
fragment_my_fragment2
java
1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.kanehiro.fragmentapplication.MyFragment2"> 11 12 <EditText 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/input_message" 16 android:layout_alignParentLeft="true" 17 android:layout_alignParentStart="true" 18 android:layout_toLeftOf="@+id/send_message" 19 android:layout_toStartOf="@+id/send_message" /> 20 21 <Button 22 android:id="@+id/send_message" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_alignParentEnd="true" 26 android:layout_alignParentRight="true" 27 android:text="SEND"/> 28 29 <ScrollView 30 android:layout_width="fill_parent" 31 android:layout_height="fill_parent" 32 android:layout_alignParentLeft="true" 33 android:layout_alignParentStart="true" 34 android:layout_below="@+id/input_message"> 35 <LinearLayout 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:orientation="vertical" 39 android:id="@+id/message_log"> 40 41 <TextView 42 android:id="@+id/memo_message" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_gravity="start" 46 android:text="memo一覧"/> 47 </LinearLayout> 48 </ScrollView> 49 50</RelativeLayout>
activity_main_xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<merge xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity"> 7 8 <FrameLayout 9 android:id="@+id/contents" 10 android:paddingBottom="@dimen/activity_vertical_margin" 11 android:paddingLeft="@dimen/activity_horizontal_margin" 12 android:paddingRight="@dimen/activity_horizontal_margin" 13 android:paddingTop="@dimen/activity_vertical_margin" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" /> 16</merge>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/01 07:38
2016/10/01 09:20 編集