Toast
自定义Toast
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mButton1; private Button mButton2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton1 = (Button) findViewById(R.id.button1); mButton1.setOnClickListener(this); mButton2 = (Button) findViewById(R.id.button2); mButton2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Toast toast = Toast.makeText(getApplicationContext(), "你真傻,", Toast.LENGTH_LONG); //添加富文本 Spanned spanned = Html.fromHtml("我<img src=''/>是<font color='#50f0'>toast</font>,你真傻", new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = getResources().getDrawable(R.mipmap.tangyan); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; } }, null); //更改文本 toast.setText(spanned); //设置显示位置 //zh toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); toast.show(); break; case R.id.button2: Toast toast2 = new Toast(getApplicationContext()); LayoutInflater inflater = getLayoutInflater(); View toastView = inflater.inflate(R.layout.my_toast_layout, null); TextView textViewTitle = (TextView) toastView.findViewById(R.id.title); TextView textViewMessage = (TextView) toastView.findViewById(R.id.textview_message); textViewTitle.setText("标题"); textViewMessage.setText("内容"); toast2.setView(toastView); toast2.setDuration(Toast.LENGTH_LONG); toast2.show(); break; default: break; } }}
Dialog
简单的Dialog
带选择项的Dialog
单选项Dialog
多项选择Dialog
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button1; private Button button2; private String[] mData = new String[]{"选项1", "选项2", "选项3", "选项4"}; private Button button3; private String msex[] = new String[]{"男", "女", "其他"}; private String sex; private Button button4; private String[] mSport = new String[]{"篮球", "足球", "游泳", "跑步", "健身"}; private boolean isCheckedHobby[] = new boolean[mSport.length]; private StringBuffer hobby; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setCountView必须放前面,否则空指针 setContentView(R.layout.activity_main); //总是写成 button1. findViewById(R.id.button1); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(this); button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(this); button4 = (Button) findViewById(R.id.button4); button4.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: //传getApplicationContext()会出错 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher).setTitle("标题").setMessage("内容").setNegativeButton("Negative", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "点击了Negative按钮", Toast.LENGTH_LONG).show(); } }).setNeutralButton("Neutral", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "点击了NeutralButton按钮", Toast.LENGTH_LONG).show(); } }).setPositiveButton("Positive", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "点击了PositiveButton按钮", Toast.LENGTH_LONG).show(); } }).create().show(); break; case R.id.button2: AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); builder1.setTitle("选择的Dialog"); builder1.setItems(mData, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "选中" + (which + 1) + "", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = builder1.create(); dialog.show(); break; case R.id.button3: AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this); builder2.setTitle("请选择性别"); //0位默认第一个选项 builder2.setSingleChoiceItems(msex, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "您选择性别为" + msex[which] + "", Toast.LENGTH_SHORT).show(); //sex就是用来传值的 sex = msex[which]; } }); builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { button3.setText("您选择性别为" + sex); } //create().show();也可以不加如果此处添加了,下面AlertDialog dialog1=builder2.create(); // dialog1.show();也存在的话,,界面上会执行两遍 }); builder2.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog1 = builder2.create(); dialog1.show(); break; case R.id.button4: AlertDialog.Builder builder3 = new AlertDialog.Builder(MainActivity.this); builder3.setTitle("请选择爱好"); builder3.setMultiChoiceItems(mSport, isCheckedHobby, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { isCheckedHobby[which] = isChecked; } }); builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { hobby = new StringBuffer(); for (int i = 0; i < isCheckedHobby.length; i++) { if (isCheckedHobby[i]) { hobby.append(mSport[i]); } } button4.setText("您选择愛好为" + hobby); } //create().show();也可以不加如果此处添加了,下面AlertDialog dialog1=builder2.create(); // dialog1.show();也存在的话,,界面上会执行两遍 }); builder3.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog3 = builder3.create(); dialog3.show(); break; default: break; } }}
版权声明:本文为博主原创文章,未经博主允许不得转载。