DialogFragment 无法使用 1 个按钮。
为什么我的代码不起作用?
我需要带有 1 个按钮的dialogFragment。
我有 2 个课程:
public class MyAlertDialogFragment extends DialogFragment {
static DialogFragment newInstance(int num) {
MyAlertDialogFragment f = new MyAlertDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.alertdialog, container, false);
Button button = (Button)v.findViewById(R.id.button1);
return v;
}
}
和活动:
public class DialogFragmentActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog();
}
void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
newFragment.show(ft, "dialog");
}
}
你如何完成这个片段?
Why doesn't my code work?
I need dialogFragment with 1 button.
I have 2 classes:
public class MyAlertDialogFragment extends DialogFragment {
static DialogFragment newInstance(int num) {
MyAlertDialogFragment f = new MyAlertDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.alertdialog, container, false);
Button button = (Button)v.findViewById(R.id.button1);
return v;
}
}
and Activity:
public class DialogFragmentActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog();
}
void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
newFragment.show(ft, "dialog");
}
}
How do you complete this fragment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要重写 onCreateView,重写 onCreateDialog:
Don't override onCreateView, override onCreateDialog: