为什么我的代码不起作用?
我需要带有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");
}
}
你如何完成这个片段?
答案 0 :(得分:1)
不要覆盖onCreateView,覆盖onCreateDialog:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity())
.setPositiveButton(android.R.string.ok, null);
return alert.create();
}