我遇到了问题。 我有一个扩展listactivity的类,它的代码是这样的:
public class QuestionListActivity extends ListActivity
{
//Members
private ImageButton bntRefresh;
private ImageButton bntSettings;
private ImageButton bntGetSurveys;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.surveylist);
initialize();
currentSurveys=new SurveyList();
}
private void initialize()
{
bntRefresh= (ImageButton) findViewById(R.id.ibtnRefresh);
bntRefresh.setImageResource(R.drawable.refresh);
bntRefresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SurveyListActivity.this, "This will refresh the survey list.", Toast.LENGTH_SHORT).show();
}
});
bntSettings= (ImageButton) findViewById(R.id.ibtnSettings);
bntSettings.setImageResource(R.drawable.settings);
bntSettings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SurveyListActivity.this, "This will open the settings.", Toast.LENGTH_SHORT).show();
}
});
bntGetSurveys= (ImageButton) findViewById(R.id.ibtnGetSurveys);
bntGetSurveys.setImageResource(R.drawable.getsurvey);
bntGetSurveys.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).create();
getSurveyAlert.setTitle("Enter QR Code");
getSurveyAlert.setMessage("Choose a source");
getSurveyAlert.setButton("Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
getSurveyAlert.setButton2("text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
});
}
class SurveyAdapter extends ArrayAdapter<Survey>
{
//-- code here for adapter
}
}
在我的初始化函数中,你可以看到我有三个图像按钮,我已经为它们实现了onClicklisteners。 对于设置和刷新按钮,我显示了一个Toast,它工作正常。 对于Get Button我已经显示了一个对话框,它进一步要求用户做某事,但我遇到的问题是对话框没有出现,我不知道为什么?
答案 0 :(得分:2)
你忘了,写行
getSurveyAlert.show();
试试这个,
bntGetSurveys.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).create();
getSurveyAlert.setTitle("Enter QR Code");
getSurveyAlert.setMessage("Choose a source");
getSurveyAlert.setButton("Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
getSurveyAlert.setButton2("text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
getSurveyAlert.create().show();
}
答案 1 :(得分:1)
调用show()方法
@Override
public void onClick(View v)
{
AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).create();
getSurveyAlert.setTitle("Enter QR Code");
getSurveyAlert.setMessage("Choose a source");
getSurveyAlert.setButton("Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
getSurveyAlert.setButton2("text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
getSurveyAlert.create().show();
}
});
答案 2 :(得分:1)
AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity.this).create(); //使用此行代替此
AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).show();