gpt4 book ai didi

java - 在 onClick 事件后创建对话框

转载 作者:行者123 更新时间:2023-12-02 06:37:56 24 4
gpt4 key购买 nike

嗨,我是第一次使用对话框。我想在单击 listView 中的项目后创建一个对话框:

    String[] listItems = {"Colour", "Font Size",};
ListView lv = (ListView) findViewById(R.id.settings_list);

lv.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems));

lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View
view, int position, long id)
{
String[] listItems = {"Colour", "Font Size",};
if(listItems[position].equals("Font Size"))
{


public dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Font Size");
}



}

}
});

我在 onCreateDialog(Bundle savingInstanceState) 处收到错误Eclipse 想要一个 ; Bundle saveInstanceState 周围,但developer.android 另有说法。有什么建议吗?

最佳答案

在 onItemClick 方法中调用此方法:

 public void showMyCustomDialog(Context context, String title, String message)
{
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//tell the Dialog to use the customalertdialog.xml as it's layout description
dialog.setContentView(R.layout.customalertdialog);
TextView titleText = (TextView) dialog.findViewById(R.id.txtTitleAlertDialog);
titleText.setText(title);
TextView txt = (TextView) dialog.findViewById(R.id.txtAlertDialog);
txt.setText(message);
Button dialogButton = (Button) dialog.findViewById(R.id.buttonAlertDialogCancel);
dialogButton.setText("OK");
dialogButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}

如下所示:

lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View
view, int position, long id)
{
String[] listItems = {"Colour", "Font Size",};
if(listItems[position].equals("Font Size"))
{
public dialog onCreateDialog(Bundle savedInstanceState)
{
showMyCustomDialog(Context context, String title, String message) // pass title, message and activity context in this method.
}
}
}
});

希望能有所帮助。

关于java - 在 onClick 事件后创建对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19455649/

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