[英]Open Link From Alert Dialog Box Button Android
我彈出一個警報框,要求用戶幫助翻譯兩個按鈕“幫助翻譯”和“關閉”,我該如何做,因此當用戶單擊“幫助翻譯”時,它將帶他們到網站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);
}
});
有關更多信息,請訪問
您可以使用getButton(BUTTON_POSITIVE);獲得按鈕。
http://developer.android.com/reference/android/app/AlertDialog.html#getButton(int)
然后,當您單擊“幫助翻譯”按鈕時,您可以執行以下操作:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.