Fragment
에는 getView()
이라는 방법이 있습니다. 그것에 에 붙어있는 한 Fragment
의 View
를 얻을 수있다. 당신이 Fragment
내부 View
를 찾고 있다면
View view = fragment.getView();
그러나 당신은 또한 단지 Activity
에서 findViewById()
그것을 얻을 수 있습니다. 다시 작동하려면 Fragment
을 Activity
에 연결해야합니다.
but 그렇게해서는 안됩니다. Fragment
외부의 어떤 것도 Fragment
내부의 무언가와 관련이 없어야합니다. Fragment
에 공용 메소드를 작성하여 상호 작용하십시오. 이런 식으로 뭔가를 시도해보십시오에서
public static class NoteFragment extends Fragment {
private EditText noteEditText;
public NoteFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes, container, false);
this.noteEditText = (EditText) rootView.findViewById(R.id.noteEditText);
return rootView;
}
// I added the following 3 methods to interact with the Fragment
public boolean isEmpty() {
final String text = this.noteEditText.getText().toString();
return text.isEmpty();
}
public String getText() {
return this.noteEditText.getText().toString();
}
public void setText(String text) {
this.noteEditText.setText(text);
}
}
그리고 지금 당신의 Activity
당신은이 작업을 수행 할 수 있습니다
public class NotesActivity extends Activity {
private int bookId;
private int chapterId;
private NoteFragment noteFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
// get data from intent that sent from home activity
bookId = getIntent().getIntExtra("book_id", -1);
chapterId = getIntent().getIntExtra("book_id", -1);
if (savedInstanceState == null) {
this.noteFragment = new NoteFragment();
getFragmentManager().beginTransaction().add(R.id.container, this.noteFragment).commit();
}
// Now you can interact with the Fragment
this.noteFragment.setText("some text");
...
if(!this.noteFragment.isEmpty()) {
String note = this.noteFragment.getText();
...
}
}
}
당신이의 무엇을 의미하는지 확실하지 않다 "그것과 상호 작용하는 조각의 공용 메소드를 작성합니다." 제발 도와주세요 –
예를 들어 보았습니다. –