gpt4 book ai didi

android - 将正/负按钮添加到 DialogFragment 对话框

转载 作者:IT王子 更新时间:2023-10-29 00:05:49 25 4
gpt4 key购买 nike

我已经写了一个DialogFragment。现在我意识到我希望它像 AlertDialog 一样有一个正面和一个负面按钮。如何在维护我编写的代码的同时实现这样的目标?

public class DoublePlayerChooser extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setStyle(DialogFragment.STYLE_NORMAL,0);



}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

return new AlertDialog.Builder(getActivity())
.setTitle("title")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
)
.create();
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.doubleplayerchooser, container, false);
getDialog().setTitle("Enter Players");

firstPlayerPicker = (ImageButton) v.findViewById(R.id.imageButton1);
firstPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){

callContactPicker(1);

}
});

secondPlayerPicker = (ImageButton) v.findViewById(R.id.ImageButton01);
secondPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){

callContactPicker(2);

}
});

loadFromFile = (ImageButton) v.findViewById(R.id.imageButton2);
loadFromFile.setOnClickListener(new OnClickListener() {
public void onClick(final View v){



}
});

firstTextfield = (EditText) v.findViewById(R.id.editText1);
secondTextfield = (EditText) v.findViewById(R.id.EditText01);

firstImage = (ImageView) v.findViewById(R.id.imageView1);
secondImage = (ImageView) v.findViewById(R.id.ImageView01);



return v;
}

最佳答案

这就是我想出来的。我删除了 onCreateView 并更改了 onCreateDialog。这个link实际上有答案,所以所有的功劳都应该去那里。我刚刚发布它以防万一有人先遇到这个问题。

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder b= new AlertDialog.Builder(getActivity())
.setTitle("Enter Players")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
);

LayoutInflater i = getActivity().getLayoutInflater();

View v = i.inflate(R.layout.doubleplayerchooser,null);

firstPlayerPicker = (ImageButton) v.findViewById(R.id.imageButton1);
firstPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){

callContactPicker(1);

}
});

secondPlayerPicker = (ImageButton) v.findViewById(R.id.ImageButton01);
secondPlayerPicker.setOnClickListener(new OnClickListener() {
public void onClick(final View v){

callContactPicker(2);

}
});

loadFromFile = (ImageButton) v.findViewById(R.id.imageButton2);
loadFromFile.setOnClickListener(new OnClickListener() {
public void onClick(final View v){



}
});

firstTextfield = (EditText) v.findViewById(R.id.editText1);
secondTextfield = (EditText) v.findViewById(R.id.EditText01);

firstImage = (ImageView) v.findViewById(R.id.imageView1);
secondImage = (ImageView) v.findViewById(R.id.ImageView01);


b.setView(v);
return b.create();
}

关于android - 将正/负按钮添加到 DialogFragment 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18601049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com