Yul*_*ima 6 android android-fragments android-studio
使用Android Studio创建新片段时,它会生成onButtonPressed(Uri)方法,如何将其挂钩到UI事件中,比如点击xml中声明的按钮?这个方法打算如何使用?
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
Run Code Online (Sandbox Code Playgroud)
片段附加到活动,这onFragmentInteraction
是您的活动用来与片段交互的回调方法
例如,以下活动实现片段中的接口
public static class YourActivity extends Activity
implements YourFragment.onFragmentInteraction{
...
public void onFragmentInteraction(Uri uri) {
// Do something with uri
}
}
Run Code Online (Sandbox Code Playgroud)
但正如TODO
建议的那样
// TODO: 重命名方法,更新参数和钩子方法到 UI 事件中
请随意根据您的需要进行调整,如果不需要则将其删除。例子:
mYourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mListener != null) {
mListener.onFragmentInteraction(Uri.parse("http://www.google.com"));
}
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5839 次 |
最近记录: |