我一直在尝试为我的应用程序创建一个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一样的建议。感谢任何试图帮助我的人。
答案 0 :(得分:2)
您可以通过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();
}
}
答案 1 :(得分:1)
对我而言,“对话框”是一个具有对话框主题的活动,请尝试以下方法:
<activity android:theme="@android:style/Theme.Dialog">
答案 2 :(得分:1)
我不建议您创建您提供的布局,因为它太复杂,无法移动。但是可以创建每行1个选项的简化版本。
您可以在the official docs中看到AlertDialog.Builder支持的所有受支持的按钮,选项等。
正如@TronicZomB指出的那样,您可以将自己的XML布局与setView(View view)一起使用。