思路:用户点击“开始测试”按钮后,会弹出一个确认对话框,用户点击确定后,自动跳转到测试题界面。
确认对话框效果图:
这也是一个alertdialog,具体代码如下:
AlertDialog.Builder builder = new Builder(
Logan_LeJiaActivity.this);
builder.setTitle("导读")
.setIcon(R.drawable.icon)
.setView(layout)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
// 进入答题界面,激活另一个activity
Intent intent = new Intent();
intent.setClass(
Logan_LeJiaActivity.this,
TestingView.class);
startActivity(intent);
}
});
builder.show();
}
});
但是要注意:在控制这个dialog的布局的时候,由于他的父控件是Logan_LeJiaActivity,而且已经对他设置了setcontentview(R.layout.main);因此这里就不能使用同样的方式再对dialog进行布局控制,而是用Inflater的方式,进行转换控制:LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.answerdialog, null);
layout.setBackgroundColor(Color.BLACK);
TextView view = (TextView) layout
.findViewById(R.id.answerdialog_textView1);
ScrollView scrollview = (ScrollView) layout
.findViewById(R.id.answerdialog_scrollView1);同样地,在调用dialog布局文件里的空间时,需要: 布局名.findviewbyid();