public class MainActivity extends AppCompatActivity {
private TextView tv_show;
private TextView tv_show1;
private LinearLayout groud;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
groud.setBackgroundColor(getResources().getColor(R.color.red1));
break;
case R.id.blue:
groud.setBackgroundColor(getResources().getColor(R.color.blue3));
break;
case R.id.green:
groud.setBackgroundColor(getResources().getColor(R.color.green2));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
groud = (LinearLayout) findViewById(R.id.groud);
initView();
}
private void initView() {
tv_show = (TextView) findViewById(R.id.tv_show);
registerForContextMenu(tv_show);
tv_show1 = (TextView) findViewById(R.id.tv_show1);
tv_show1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, tv_show1);
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.red:
Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
tv_show1.setTextColor(getResources().getColor(R.color.red1));
break;
case R.id.green:
Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show();
tv_show1.setTextColor(getResources().getColor(R.color.green2));
break;
case R.id.blue:
Toast.makeText(MainActivity.this, "蓝色", Toast.LENGTH_SHORT).show();
tv_show1.setTextColor(getResources().getColor(R.color.blue3));
break;
}
return false;
}
});
popupMenu.show();
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.red:
Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
tv_show.setTextColor(getResources().getColor(R.color.red1));
break;
case R.id.green:
Toast.makeText(this, "绿色", Toast.LENGTH_SHORT).show();
tv_show.setTextColor(getResources().getColor(R.color.green2));
break;
case R.id.blue:
Toast.makeText(this, "蓝色", Toast.LENGTH_SHORT).show();
tv_show.setTextColor(getResources().getColor(R.color.blue3));
break;
}
return super.onContextItemSelected(item);
}
}
ListView显示数据,微信QQ聊天
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jia = (TextView) findViewById(R.id.jia);
jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_custom, null);
Button button= inflate.findViewById(R.id.sao);
Button button1= inflate.findViewById(R.id.tianjia);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "扫一扫", Toast.LENGTH_SHORT).show();
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "添加好友", Toast.LENGTH_SHORT).show();
}
});
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(300);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(jia, 0, 0);
}
});
initView();
}
private void initView() {
Lv_1 = (ListView) findViewById(R.id.Lv_1);
layout_1 = (RelativeLayout) findViewById(R.id.layout_1);
final String[] name = getResources().getStringArray(R.array.name);
final String[] number = getResources().getStringArray(R.array.number);
for (int i = 0; i < name.length; i++) {
HashMap<String, Object> map = new HashMap<>();
map.put("name", name[i]);
map.put("number", number[i]);
lists.add(map);
}
myAdapter = new MyAdapter(this, lists);
Lv_1.setAdapter(myAdapter);
Lv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.zhiding:
lists.add(0, lists.get(position));
lists.remove(position + 1);
myAdapter.notifyDataSetChanged();
break;
case R.id.shanchu:
lists.remove(position);
myAdapter.notifyDataSetChanged();
break;
}
return false;
}
});
popupMenu.show();
return true;
}
});
layout_2 = (LinearLayout) findViewById(R.id.layout_2);
layout_3 = (LinearLayout) findViewById(R.id.layout_3);
lianxiren = (RadioButton) findViewById(R.id.lianxiren);
lianxiren.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout_2.setVisibility(View.GONE);
layout_3.setVisibility(View.VISIBLE);
}
});
xiaoxi = (RadioButton) findViewById(R.id.xiaoxi);
xiaoxi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout_3.setVisibility(View.GONE);
layout_2.setVisibility(View.VISIBLE);
}
});
}
}
Adapter:
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Map<String,Object>> lists;
private LayoutInflater layoutInflater;
public MyAdapter(Context context, List<Map<String, Object>> lists) {
this.context = context;
this.lists = lists;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView==null){
convertView=layoutInflater.inflate(R.layout.layout_item,null);
viewHolder=new ViewHolder();
viewHolder.imageView=convertView.findViewById(R.id.iv_image);
viewHolder.name=convertView.findViewById(R.id.name);
viewHolder.number=convertView.findViewById(R.id.number);
convertView.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(lists.get(position).get("name").toString());
viewHolder.number.setText(lists.get(position).get("number").toString());
RequestOptions options=new RequestOptions();
options.circleCrop();
Glide.with(context).load(R.mipmap.aa).apply(options).into(viewHolder.imageView);
return convertView;
}
class ViewHolder{
private ImageView imageView;
private TextView name,number;
}
}
菜单列表
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_1 = (Button) findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_1:
PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_tupian, null);
Button bule = inflate.findViewById(R.id.blue);
Button green = inflate.findViewById(R.id.green);
Button red = inflate.findViewById(R.id.red);
final ImageView imageView = inflate.findViewById(R.id.iv_123);
bule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor("#0091F1"));
}
});
green.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor("#1AFF00"));
}
});
red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor("#EC0000"));
}
});
popupWindow.setContentView(inflate);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.5f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes1 = getWindow().getAttributes();
attributes1.alpha=1.0f;
getWindow().setAttributes(attributes1);
}
});
View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
popupWindow.showAtLocation(inflate1,Gravity.CENTER,0,0);
break;
}
}
}
QQ聊天
public class MainActivity extends AppCompatActivity {
private ListView message;
private TextView txl;
private TextView find;
private TextView person;
private TextView jia;
private int num=0;
private List<String> list = new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
message = (ListView) findViewById(R.id.message);
txl = (TextView) findViewById(R.id.txl);
find = (TextView) findViewById(R.id.find);
person = (TextView) findViewById(R.id.person);
jia = (TextView) findViewById(R.id.jia);
for (int i = 1; i <= 20; i++) {
list.add("我是第:"+i+"条数据!");
num = i;
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
message.setAdapter((ListAdapter) adapter);
message.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
message.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("是否删除");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list.remove(position);
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
return true;
}
});
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_message:
message.setVisibility(View.VISIBLE);
txl.setVisibility(View.GONE);
find.setVisibility(View.GONE);
person.setVisibility(View.GONE);
break;
case R.id.button_txl:
message.setVisibility(View.GONE);
txl.setVisibility(View.VISIBLE);
find.setVisibility(View.GONE);
person.setVisibility(View.GONE);
break;
case R.id.button_find:
message.setVisibility(View.GONE);
txl.setVisibility(View.GONE);
find.setVisibility(View.VISIBLE);
person.setVisibility(View.GONE);
break;
case R.id.button_person:
message.setVisibility(View.GONE);
txl.setVisibility(View.GONE);
find.setVisibility(View.GONE);
person.setVisibility(View.VISIBLE);
break;
}
}
public void jia(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, jia);
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Toast.makeText(MainActivity.this, "添加数据", Toast.LENGTH_SHORT).show();
list.add("我是第:"+(++num)+"条数据!");
adapter.notifyDataSetChanged();
break;
case R.id.clear:
Toast.makeText(MainActivity.this, "清空数据", Toast.LENGTH_SHORT).show();
list.clear();
adapter.notifyDataSetChanged();
break;
}
return false;
}
});
popupMenu.show();
}
}