我希望有一个对话框,可以听取短和长点击(当用户将手指放在某个项目上并通过播放音频文件的预览对其做出反应时,无需关闭DialogInterface(为用户提供一些便利)。
检查DialogInterface class上的API后,我发现它只提供了一个OnClickListener。是否可以使DialogInterface对象监听两者?
以下是代码:
public class SetMetronomeDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Pick a tempo");
builder.setItems(R.array.tempoArray, new DialogInterface.OnClickListener() {
// this code works fine since it's from the Android Developer site :)
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
Toast.makeText(getActivity(), "" + which, Toast.LENGTH_SHORT).show();
}
// this is my 'dream method', doesn't work of course...
public void onLongClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "" + which, Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}