//这个是布局整体,包括按钮,背景,边框距离等等
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
"@+id/textview"
android:text="你来试试"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
_____________________________________________________
这个是针对第二个按钮布局弹出来的信息的格式
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
"@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="什么鬼"//这是标题的内容,可在activity中刷新
/>
"@+id/toast2_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"//加一张图片
/>
"wrap_content"
android:layout_height="wrap_content"
android:text="我真的是好人"/>
<2>
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {//注意继承的方法和实现的接口
private Button mButton;
private Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(this);
mButton2 = (Button) findViewById(R.id.button2);
mButton2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button :
Toast toast = Toast.makeText(getApplicationContext(),"我是一个好人",Toast.LENGTH_SHORT);
//下面这一块是富文本
————————————————————————————————————
Spanned spanned = Html.fromHtml("我是一个好人,", new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
},null);
++++++++++++++++++++++++++++++++++++++++
toast.setText(spanned);
toast.show();
toast.setGravity(Gravity.CENTER,0,0);
break;
case R.id.button2:
Toast toast2 = new Toast(getApplicationContext());
LayoutInflater inflater = getLayoutInflater();
View toastView = inflater.inflate(R.layout.item_toast, null);
TextView textview_title = (TextView) toastView.findViewById(R.id.textview_title);
textview_title.setText("我是标题");//刷新标题内容
toast2.setView(toastView);
toast2.show();
toast2.setDuration(Toast.LENGTH_LONG);//显示时间长短
break;
default:
break;
}
}
}
<1>layout下的布局文件
//这里面给出四个按钮的布局
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
"@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dialog点击测试" />
"@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:text="弹出可选择的Dialog" />
"@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:text="单选框的dialog" />
"@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多项选择的dialog"
android:layout_below="@+id/button3"/>
<2>MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//声明变量,初始化
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private String[] mData = {"第一个包子", "第二个包子", "第三个包子", "第四个包子"};
private String[] mSex = {"男", "女", "呵呵"};
private String[] mHobby = {"画画", "唱歌", "跳舞", "游泳", "旅游"};
private boolean[] mIsCheckedHobby;
private String sex;
private StringBuffer hobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//分别找到对应的按钮建立点击事件
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {//针对不同的点击按钮调用其方法
case R.id.button1:
showCheck();
break;
case R.id.button2:
showSelect();
break;
case R.id.button3:
showOnecheck();
break;
case R.id.button4:
showMoreCheck();
break;
default:
break;
}
}
——————————————————————————————————————————————
//点击事件的方法
public void showCheck() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//先创建对象,然后添加标题,内容,选择按钮等 builder.setIcon(R.mipmap.ic_launcher).setTitle("我是dialog的标题").setMessage("我是主角,我叫内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//点击每个选择项时都会显示一段时间提示你选择的是什么
Toast.makeText(getApplicationContext(), "点击的是NegativeButton", Toast.LENGTH_LONG).show();
}
}).setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击的是NeutralButton", Toast.LENGTH_LONG).show();
}
}).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击的是PositiveButton", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
这个方法对应下面的视频一,效果见下面
———————————————————————————————————————————————————————
这是弹出可选择的dialog方法
public void showSelect() {
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setTitle("选择的dialog");//在弹出的框中添加标题,也可以添加图片如上一个方法
builder2.setItems(mData, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "选中了第" + which+1 + "个包子", Toast.LENGTH_SHORT).show();//选择后弹出你选择的数据
}
});
AlertDialog dialog2 = builder2.create();
dialog2.show();
}
这个方法对应下面的视频二
——————————————————————————————————————————————
单项选择的Dialog
public void showOnecheck() {
AlertDialog.Builder builder3 = new AlertDialog.Builder(MainActivity.this);
builder3.setTitle("选择角色性别");
builder3.setSingleChoiceItems(mSex, 3, new DialogInterface.OnClickListener() {//这里数字3,默认时一个都不选,0默认弹出时第一个已经选上了
@Override
public void onClick(DialogInterface dialog, int which) {
sex = mSex[which];//选择性别,复制给SEX
Toast.makeText(getApplicationContext(), "选中的性别是" + mSex[which], Toast.LENGTH_SHORT).show();
}
});
builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder3.setNeutralButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButton3.setText("您的性别是" + sex);
}//点击确定后,刷新button3的文本内容
});
AlertDialog dialog3 = builder3.create();
dialog3.show();
}
这个方法的实现效果见视频三
————————————————————————————————
多项选择的Dialog
public void showMoreCheck() {
mIsCheckedHobby = new boolean[mHobby.length];//一开始初始化所有的爱好,默认是false,即一个也没选
AlertDialog.Builder builder4 = new AlertDialog.Builder(MainActivity.this);
builder4.setTitle("选择您的爱好");
builder4.setMultiChoiceItems(mHobby, mIsCheckedHobby, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mIsCheckedHobby[which] = isChecked;
}
});
builder4.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder4.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
hobby = new StringBuffer();
for (int i = 0; i < mIsCheckedHobby.length; i++) {
if (mIsCheckedHobby[i]) {
hobby.append(mHobby[i]);//将选择的每个爱好排列起来
}
}
mButton4.setText("您的爱好是" + hobby);//刷新按钮的文本,选择的爱好排列在“您的爱好是”的后面
}
});
AlertDialog dialog4 = builder4.create();
dialog4.show();
}
}
这个方法的实现效果见视频四