gpt4 book ai didi

android - DialogFragments 供用户输入数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:27:13 24 4
gpt4 key购买 nike

我一直在尝试为我的应用制作一个 dialogFragment,但由于 Internet 上缺乏可用示例,事实证明这很难做到。下面是一张我想要的类似图片。 dialogFragment Example

具有与上图类似的设计会很棒。我不确定我在做什么是否正确,到目前为止这就是我所做的。

My attempt at dialogFragment

我不知道如何像第一张图片那样布置 dialogFragment,而且我不确定如何获取用户可以输入数据的字段。以下是我目前拥有的代码。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class Reminders extends Activity
{

Button add, edit, remove;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reminders);
initializeVariables();

add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog();
}
});

List<ListViewItem> items = new ArrayList<Reminders.ListViewItem>();
items.add(new ListViewItem()
{
{
ThumbnailResource = R.drawable.ic_launcher;
Title = "Item1 Title";
Date = "Item1 Date";
Time = "Item1 Time";
Amount = "£0.00";
}
});
items.add(new ListViewItem()
{
{
ThumbnailResource = R.drawable.ic_launcher;
Title = "Item2 Title";
Date = "Item2 Date";
Time = "Item2 Time";
Amount = "£0.00";
}
});

CustomListViewAdapter adapter = new CustomListViewAdapter(this, items);
lv.setAdapter(adapter);

}

private void initializeVariables()
{
add = (Button) findViewById(R.id.bAdd);
edit = (Button) findViewById(R.id.bEdit);
remove = (Button) findViewById(R.id.bRemove);
lv = (ListView) findViewById(R.id.LVReminder);
}

class ListViewItem
{
public int ThumbnailResource;
public String Title;
public String Date;
public String Time;
public String Amount;
}


void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment
.newInstance(R.string.dialog_title);
newFragment.show(getFragmentManager(), "New");
}

public void doPositiveClick() {
// Do stuff here.
Log.i("FragmentAlertDialog", "Positive click!");
}

public void doNegativeClick() {
// Do stuff here.
Log.i("FragmentAlertDialog", "Negative click!");
}

public static class MyAlertDialogFragment extends DialogFragment
{

public static MyAlertDialogFragment newInstance(int title)
{
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
int title = getArguments().getInt("title");

return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((Reminders) getActivity())
.doPositiveClick();
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((Reminders) getActivity())
.doNegativeClick();
}
}).create();
}
}
}

对于如何获得像我提供的第一张图片中那样的 DialogFragment 的任何建议,我将不胜感激。感谢任何试图帮助我解决这个问题的人。

最佳答案

您可以通过 XML 文档创建您希望实现的布局。然后从那里你可以改变你的 onCreateDialog 如下:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
int title = getArguments().getInt("title");
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.my_dialog_layout, null);

return new AlertDialog.Builder(getActivity())
.setView(v)
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((Reminders) getActivity())
.doPositiveClick();
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((Reminders) getActivity())
.doNegativeClick();
}
}).create();
}
}

关于android - DialogFragments 供用户输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17754310/

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