因此,我一直在阅读有关 Android 中的对话框的内容,并且很好奇如何在我当前的代码中实现这一点。
我按照 android文档创建了自定义布局。我将它创建为一个新类。
public class AddSiteDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.main, null))
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//To change body of implemented methods use File | Settings | File Templates.
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AddSiteDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
现在,我希望在单击 ViewPager 布局内的按钮时显示该对话框。在不同的班级。public class FieldsActivity extends Activity {
我有 ViewPager。
private class MyPagerAdapter extends PagerAdapter {
final ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = (ListView)findViewById(android.R.id.list);
final ArrayList<String> siteList = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(getBaseContext(), R.layout.list_row, siteList);
if(lv == null) Log.v("lv", "null");
else Log.v("aa", "null");
if (lv != null) {
lv.setAdapter(aa);
}
int resId = 0;
View view = null;
switch (position) {
case 0:
resId = R.layout.field01;
view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
case 1:
resId = R.layout.add_site;
view = inflater.inflate(resId, null);
Button addSiteButton = (Button)view.findViewById(R.id.addSiteButton);
((ViewPager) collection).addView(view, 0);
addSiteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//I want to click this button and the dialog show up
}
});
return view;
我将如何在那里实现这一点。
我对此很陌生,所以我试图弄清楚事情是如何相互交流的。
谢谢!