![这里写图片描述](https://img-blog.csdn.net/20150827183837982)
Toast
自定义Toast
![这里写图片描述](https://img-blog.csdn.net/20150827185607348)
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);
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
![这里写图片描述](https://img-blog.csdn.net/20150827182554660)
带选择项的Dialog
![这里写图片描述](https://img-blog.csdn.net/20150827182100393)
单选项Dialog
![这里写图片描述](https://img-blog.csdn.net/20150827182150705)
多项选择Dialog
![这里写图片描述](https://img-blog.csdn.net/20150827182230337)
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);
setContentView(R.layout.activity_main);
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:
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("请选择性别");
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 = msex[which];
}
});
builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
button3.setText("您选择性别为" + sex);
}
});
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);
}
});
builder3.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog3 = builder3.create();
dialog3.show();
break;
default:
break;
}
}
}