
どうぞよろしくお願いします。FragmentのjavaクラスでfindViewById()を使いたいのですがどうすれば良いのでしょうか?
MyFragmentのjavaクラスです。
java
1package com.example.kanehiro.fragmentapplication; 2 3 4import android.os.Bundle; 5import android.support.v4.app.Fragment; 6import android.support.v4.app.FragmentManager; 7import android.view.LayoutInflater; 8import android.view.View; 9import android.view.ViewGroup; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.LinearLayout; 13import android.widget.TextView; 14import android.widget.Toast; 15 16 17public class MyFragment2 extends Fragment implements View.OnClickListener { 18 19 //とりあえずここにさらなるステップの変数 20 private EditText mInputMessage; 21 private Button mSendMessage; 22 private LinearLayout mMessageLog; 23 private TextView mMemoMessage; 24 25 26 27 public MyFragment2() { 28 29 } 30 31 32 @Override 33 public View onCreateView(LayoutInflater inflater, ViewGroup container, 34 Bundle savedInstanceState) { 35 View view=inflater.inflate(R.layout.fragment_my_fragment2,container,false); 36 37 38 //何に使うのかわからん Button btn = (Button) view.findViewById(R.id.button); 39 40 view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){ 41 @Override 42 public void onClick(View v){ 43 44 45 46 } 47 }); 48 return view; 49 } 50 51 52 @Override 53 public void onClick(View v) { 54 55 if(v.equals(mSendMessage)) { 56 57 if(mSendMessage==null||mSendMessage.length()==0){ 58 59 60 61 Toast.makeText(getActivity(),"文字が入力されていません",Toast.LENGTH_SHORT).show(); 62 }else { 63 //新しくエラーを消すために作った場所 64 //XMLのViewを初期化する 65 66 67 // ActivityからFragmentを取得 68 FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 69 Fragment fragment = fragmentManager.findFragmentById(R.id.send_message); 70 // FragmentのViewを取得 71 View view = fragment.getView(); 72 //この下のコードに Can not resolve method 'findViewById'が出ます。 73 74 mInputMessage = (EditText)view.findViewById(R.id.input_message);//ユーザーが入力するフィールド 75 mSendMessage = (Button) view.findViewById(R.id.send_message);//SENDボタン 76 mMessageLog = (LinearLayout)view.findViewById(R.id.message_log);//入力履歴を表示するレイアウト 77 mMemoMessage = (TextView) view.findViewById(R.id.memo_message);//メモの履歴 78 //この次はデータベースや 79 mSendMessage.setOnClickListener(this); 80 } 81 } 82 83 84 } 85} 86 87 88```fragment_my_fragment2.xmlです。 89```xml 90<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 91 xmlns:tools="http://schemas.android.com/tools" 92 android:layout_width="match_parent" 93 android:layout_height="match_parent" 94 android:orientation="vertical" 95 android:paddingBottom="@dimen/activity_vertical_margin" 96 android:paddingLeft="@dimen/activity_horizontal_margin" 97 android:paddingRight="@dimen/activity_horizontal_margin" 98 android:paddingTop="@dimen/activity_vertical_margin" 99 tools:context="com.example.kanehiro.fragmentapplication.MyFragment2"> 100 101 102 <EditText 103 android:layout_width="wrap_content" 104 android:layout_height="wrap_content" 105 android:id="@+id/input_message" 106 android:layout_alignParentLeft="true" 107 android:layout_alignParentStart="true" 108 android:layout_toLeftOf="@+id/send_message" 109 android:layout_toStartOf="@+id/send_message" /> 110 111 112 <Button 113 android:id="@+id/send_message" 114 android:layout_width="wrap_content" 115 android:layout_height="wrap_content" 116 android:layout_alignParentEnd="true" 117 android:layout_alignParentRight="true" 118 android:text="SEND"/> 119 120 <ScrollView 121 android:layout_width="fill_parent" 122 android:layout_height="fill_parent" 123 android:layout_alignParentLeft="true" 124 android:layout_alignParentStart="true" 125 android:layout_below="@+id/input_message"> 126 <LinearLayout 127 android:layout_width="fill_parent" 128 android:layout_height="wrap_content" 129 android:orientation="vertical" 130 android:id="@+id/message_log"> 131 132 <TextView 133 android:id="@+id/memo_message" 134 android:layout_width="wrap_content" 135 android:layout_height="wrap_content" 136 android:layout_gravity="start" 137 android:text="memo一覧"/> 138 139 </LinearLayout> 140 </ScrollView> 141 142</RelativeLayout> 143 144
どうぞよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー