第二天 花式进度条
作业一
花式进度条
其中包含 单选多选进度条,普通警告进度条,日期时间进度
条,实时进度进度条,自定义普通进度条
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void clicks(View view) {
Calendar calendar = Calendar.getInstance();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (view.getId()){
case R.id.button_alert:
builder.setMessage("确定要删除吗?");
builder.setTitle("提示信息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
break;
//日期对话框
case R.id.button_calendar:
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(Main2Activity.this, "选择成功", Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
break;
//自定义对话框
case R.id.button_custom:
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_list_item, null);
builder.setView(inflate);
AlertDialog dialog1 = builder.create();
dialog1.show();
break;
//时间对话框
case R.id.button_Timer:
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(Main2Activity.this, "选择成功", Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
break;
//单选按钮
case R.id.button_single:
final String[] str = {"开勒普","爱迪生","泰尔斯","蜂鸟"};
builder.setSingleChoiceItems(str, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this, "您选择了:"+str[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this, "选择成功", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this, "取消成功", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog2 = builder.create();
dialog2.show();
break;
//多选按钮
case R.id.button_multi:
final String[] items = {"瓦尔特","西林","空无","真理"};
boolean[] flags = {false,false,false,false};
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(Main2Activity.this, "你点击了:"+items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this, "选择成功", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this, "取消成功", Toast.LENGTH_SHORT).show();
}
});
AlertDialog d = builder.create();
d.show();
break;
case R.id.button_progress:
final ProgressDialog dia = new ProgressDialog(this);
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dia.setProgress(0);
dia.setMax(100);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if(progress == 100){
dia.dismiss();
timer.cancel();
}else{
dia.setProgress(progress += 20);
}
}
},0,1000);
dia.show();
break;
}
}
}
作业二
自定义超级进度条
package com.example.work;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
MyDialog dialog = new MyDialog(this);
dialog.setLeftButton(new MyDialog.LeftButton() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "我来吐槽", Toast.LENGTH_SHORT).show();
}
});
dialog.setRightButton(new MyDialog.RightButton() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "记得五星哦", Toast.LENGTH_SHORT).show();
}
});
dialog.setDismissButton(new MyDialog.DismissButton() {
@Override
public void onClick() {
}
});
dialog.show();
}
}
自定义对话框布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="500dp">
<LinearLayout
android:background="#14B6FF"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_dimiss"
android:textColor="#fff"
android:text="关闭"
android:layout_gravity="center|right"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="评价一下吧"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<TextView
android:textSize="20sp"
android:text="您已经完成本次订单~快去评价赢好礼或者返现吧~~"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="70dp">
<Button
android:id="@+id/button_left"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="我要吐槽"
/>
<Button
android:id="@+id/button_right"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="我要评价"
android:textColor="#14B6FF"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
作业三
ListView
main方法
package com.example.app2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private String[] name = {"小a","小b","小c","小d","小e","小f","小g","小h","小i","小j"};
private int[] icon = {R.mipmap.e,R.mipmap.eight,R.mipmap.five,R.mipmap.four,R.mipmap.header,R.mipmap.one,R.mipmap.seven,R.mipmap.three,R.mipmap.six,R.mipmap.two};
private String[] tel = {"111","112","113","114","115","116","117","118","119","120"};
private ListView listView;
private ArrayList<Map<String,Object>> funnyList = new ArrayList<>();
private MyBaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
for(int i = 0 ; i < 10 ; i++){
Map<String,Object> map = new HashMap<>();
map.put("name",name[i]);
map.put("icon",icon[i]);
map.put("tel",tel[i]);
funnyList.add(map);
}
}
private void initView() {
listView = findViewById(R.id.list_view);
adapter = new MyBaseAdapter(funnyList,this,listView);
listView.setAdapter(adapter);
}
}
BaseAdapter中
package com.example.app2;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
public class MyBaseAdapter extends BaseAdapter {
private ArrayList<Map<String,Object>> totalList;
private Context context;
private ListView listView;
private LayoutInflater layoutInflater;
public MyBaseAdapter(ArrayList<Map<String, Object>> totalList, Context context, ListView listView) {
this.totalList = totalList;
this.context = context;
this.listView = listView;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return totalList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.layout_list_item,null);
holder.textView_tel = convertView.findViewById(R.id.text_tel);
holder.textView_title = convertView.findViewById(R.id.text_title);
holder.imageView_pic = convertView.findViewById(R.id.image_pic);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView_title.setText(totalList.get(position).get("name").toString());
holder.textView_tel.setText(totalList.get(position).get("tel").toString());
Glide.with(context).load(totalList.get(position).get("icon")).into(holder.imageView_pic);
myListView(position,holder);
myImageView(position,holder);
return convertView;
}
private void myImageView(int position, final ViewHolder holder) {
holder.imageView_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopMenu(holder.imageView_pic);
}
});
}
private void showPopMenu(ImageView imageView_pic) {
PopupMenu popupMenu = new PopupMenu(context, imageView_pic);
popupMenu.inflate(R.menu.layout_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.blue:
Toast.makeText(context, "蓝色", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
private void myListView(int position, ViewHolder holder) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
switch (position) {
case 0:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("姓名:" + totalList.get(position).get("name") + ",电话号:" + totalList.get(position).get("tel"));
builder.setTitle("提示信息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
break;
case 1:
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(context, "您选择的是"+year+","+month+","+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
break;
case 2:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setTitle("提示信息");
builder1.setMessage("是否删除?");
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
totalList.remove(position);
notifyDataSetChanged();
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder1.create();
alertDialog.show();
break;
case 3:
AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
final String[] str = {"一块钱","二块钱","三块钱","四块钱"};
builder2.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "您选中的是"+str[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog1 = builder2.create();
dialog1.show();
break;
case 4:
AlertDialog.Builder builder3 = new AlertDialog.Builder(context);
final String[] sts = {"阿达","而是","打算","肺癌"};
boolean[] flags = {false,false,false,false};
builder3.setMultiChoiceItems(sts, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(context, "您选中是"+sts[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog2 = builder3.create();
dialog2.show();
break;
}
}
});
}
static class ViewHolder{
TextView textView_title;
TextView textView_tel;
ImageView imageView_pic;
}
}
作业四
创建一个TextView 点击改变字体颜色
package com.example.app2;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Test2_menu extends AppCompatActivity {
private TextView textView;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2_menu);
textView = findViewById(R.id.text_view);
linearLayout = findViewById(R.id.layout_background);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.red:
linearLayout.setBackgroundColor(Color.rgb(255,0,0));
break;
case R.id.blue:
linearLayout.setBackgroundColor(Color.rgb(0,0,255));
break;
case R.id.green:
linearLayout.setBackgroundColor(Color.rgb(0,255,0));
break;
}
return super.onOptionsItemSelected(item);
}
}
作业五
ContextMenu 和 PopMenu
package com.example.app2;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
public class Test_Context extends AppCompatActivity {
private TextView textView,textView_pop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test__context);
textView = findViewById(R.id.text_views);
textView_pop = findViewById(R.id.text_popMenu);
//为控件添加长按属性并且将菜单绑定到这个控件上
registerForContextMenu(textView);
textView_pop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopMenu();
}
});
}
private void showPopMenu() {
PopupMenu popupMenu = new PopupMenu(this, textView_pop);
popupMenu.inflate(R.menu.layout_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:
textView_pop.setTextColor(Color.rgb(0,0,255));
break;
case R.id.red:
textView_pop.setTextColor(Color.rgb(255,0,0));
break;
case R.id.green:
textView_pop.setTextColor(Color.rgb(0,255,0));
break;
}
return false;
}
});
popupMenu.show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.layout_menu,menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.red:
textView.setTextColor(Color.rgb(255,0,0));
break;
case R.id.blue:
textView.setTextColor(Color.rgb(0,0,255));
break;
case R.id.green:
textView.setTextColor(Color.rgb(0,255,0));
break;
}
return super.onContextItemSelected(item);
}
}
这是Menu 文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="蓝色" android:id="@+id/blue" app:showAsAction="never" />
<item android:title="红色" android:id="@+id/red" />
<item android:title="绿色" android:id="@+id/green" />
</menu>