//这是显示列表
的主要活动 public class MainActivity extends ListActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView=(ListView) findViewById(R.id.times_list);
timeTrackerAdapter=new TimeTrackerAdapter();
listView.setAdapter(timeTrackerAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//how to show a popup or dialog box
//my popup layout file is popup_layout_file.xml
}
});
}
}
答案 0 :(得分:1)
一个简单的例子是:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle("Dialog message here");
builder.setMessage("Dialog message here");
builder.create().show();
}
详细了解对话框here。
答案 1 :(得分:1)
你提到对话框/弹出窗口有你自己的xml布局,这里的另一个解决方案与Jarvis的解决方案略有不同。
public class SomeDialog extends DialogFragment {
public interface SomeDialogListener {
void onSomethingChanged(some argument);
}
private static final String DIALOG_ARGUMENT_SOMEARGUMENT= "someTag";
private SomeDialogListener mListener;
public static SomeDialog newInstance(...params) {
SomeDialog someDialog= new SomeDialog();
Bundle args= new Bundle();
args.putInt(DIALOG_ARGUMENT_DURATION, minutes);
someDialog.setArguments(args);
return someDialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle arguments= getArguments();
int duration= arguments.getInt(DIALOG_ARGUMENT_SOMEARGUMENT, DEFAULT_VALUE);
View view= inflater.inflate(R.layout.template_edit_duration_dialog, container);
getDialog().setTitle(R.string.YourTitle);
setupOKButton(view);
setupCancelButton(view);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener= (SomeDialogListener) activity;
} catch (ClassCastException e) {
throw new IllegalStateException(activity.toString() + " must implement SomeDialogListener");
}
}
private void setupOKButton(View view) {
final View okButton= view.findViewById(R.id.okButton); // From layout
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Do something, dismiss dialog ( e.g. getDialog().dismiss(); or call listener mListener.onSomethingChanged(argument) )
}
});
}
private void setupCancelButton(View view) {
final View cancelButton= view.findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Do something, dismiss dialog (e.g. getDialog().dismiss();)
}
});
}
现在可以使用SomeDialog.newInstance(params)实现对话框.show(FragmentManager,DialogTag(可选)) 使用Dialog的类也应该实现你创建的dialogListener。
如果您只需要一个对话框来通知用户您可以使用AlertDialog,或者只是使用像Jarvis建议的对话框。
答案 2 :(得分:0)
如果你想使用自己的xml布局的自定义弹出窗口,我重新命令你使用一个Dialog并使用setContentView(),如下所示:
Dialog dialog = new Dialog(PopupActivity.this);
dialog.setContentView(R.layout.my_popup);
dialog.setTitle("My first customize popup");
TextView textView = (TextView)dialog.findViewById(R.id.textViewPopup);
textView.setText("bonjour!\My first customize popup!!");
dialog.show();
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/imageViewPopup"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:src="@drawable/ic_amstrong" />
<TextView
android:id="@+id/textViewPopup"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:padding="10dp"
android:text="my first customize popup" />
</LinearLayout>
答案 3 :(得分:0)
AlertDialog.Builder builder = new Builder(MainActivity.this);
String message = "Selected " + arg2+ " item";
builder.setMessage(message);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Click cancel button
}
});
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Click ok button
}
});
builder.create().show();
答案 4 :(得分:0)
AlertDialog.builder dialog = new AlertDialog.Builder(YourActivity.this);
builder.setTitle("Title");
builder.setMessage("message to show");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
builder.create().show();
答案 5 :(得分:0)
活动也可以显示为弹出/警报对话框。只需使用弹出式布局文件创建活动,然后在requestWindowFeature(Window.FEATURE_NO_TITLE)
之前添加setContentView
。
将android:theme="@android:style/Theme.Dialog"
添加到清单中的弹出活动。
照常从onItemClickListener调用意图
Intent intent = new Intent(CurrentActivity.this, PopupActivity.class);
startActivity(intent);
希望它有所帮助。