我有一个弹出的警告框,要求用户帮助翻译两个按钮“帮助翻译”和“关闭”,我该如何做到这一点,以便当用户单击“帮助翻译”时,会将他们带到网站 example.com 。
这可以通过警报对话框来完成还是我必须使用自定义框制作布局文件
以及如何将“帮助翻译”按钮设置为左侧,将“关闭”按钮设置为右侧
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
//noinspection SimplifiableIfStatement
case R.id.action_about:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(getResources().getString(R.string.app_name));
builder.setMessage(getResources().getString(R.string.about_text));
builder.setNeutralButton("Close", null);
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();
return true;
case R.id.action_translate:
builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(getResources().getString(R.string.app_name));
builder.setMessage(getResources().getString(R.string.translate_text));
builder.setPositiveButton ("Help Translate", null);
builder.setNeutralButton("Close", null);
builder.setCancelable(true);
alert = builder.create();
alert.show();
return true;
最佳答案
为 PositiveButton
和 NegativeButton
实现 onClickListner()
,例如
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
builder.dismiss();
}
});
和
builder.setPositiveButton("Help Translate", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
builder.dismiss();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(browserIntent);
}
});
了解更多信息Go to
关于java - 从警报对话框按钮打开链接 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30024635/