findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); dialog.setTitle("title"); dialog.setMessage("message"); dialog.setButton("去百度", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Uri uri = Uri.parse("http://www.baidu.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); MainActivity.this.startActivity(intent); } }); dialog.setButton2("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show(); } });
调用一个AlertDialog,显示选择跳到baidu首页,关闭则不做任何事情
view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LayoutInflater inflater1 = getActivity().getLayoutInflater(); final View View_Dialog1 = inflater.inflate(R.layout.fragment_main, null); final EditText text = (EditText) View_Dialog1.findViewById(R.id.fragment_edit); AlertDialog dialog = new AlertDialog.Builder(getActivity()).create(); dialog.setView(View_Dialog1); dialog.setTitle("title"); dialog.setButton("Do a toast", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str = text.getText().toString(); Toast.makeText(getActivity(), "this is a ...... " + str, Toast.LENGTH_SHORT).show(); } }); dialog.show(); } });
自定义一个布局文件去加载。获得布局文件中的控件,并获得edittext的值。