1.掌握三种Menu
第一种:OptionMenu选项菜单:
创建菜单:(Android提供了两种创建菜单的方式)
1、在java代码创建菜单;
2、使用xml资源文件创建菜单(res/menu目录下)。建议使用后者。
XML资源文件中定义菜单:
1、普通菜单:
<item
android:id="@+id/menu_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="关于"/>
2、二级普通菜单:
<item
android:id="@+id/menu_group2"
android:orderInCategory="1"
android:showAsAction="never"
android:title="文字颜色">
<menu >
<group>
<item android:id="@+id/font_red" android:title="red" />
<item android:id="@+id/font_green" android:title="green"></item>
<item android:id="@+id/font_blue" android:title="blue"></item>
<item android:id="@+id/font_yellow" android:title="yellow"></item>
</group>
</menu>
</item>
3、二级可选项菜单:
<item
android:id="@+id/menu_group1"
android:orderInCategory="1"
android:showAsAction="never"
android:title="文字尺寸">
<menu ><!-- android:checkableBehavior 属性有三个可选值:all为多选,single为单选,none为不可选。为none时就是一个普通菜单 --><group android:checkableBehavior="single">
<item android:id="@+id/font_10" android:title="10sp"></item>
<item android:id="@+id/font_15" android:title="15sp"></item>
<item android:id="@+id/font_20" android:title="20sp"></item>
<item android:id="@+id/font_25" android:title="25sp"></item>
<item android:id="@+id/font_30" android:title="30sp"></item>
</group>
</menu>
</item>
(四)、菜单操作:
onOptionsItemSelected(MenuItem item)回调方法。
核心代码如下:利用以上定义的xml菜单文件,实现通过菜单选项对文字的大小和颜色进行控制。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.font_20:
text_main_info.setTextSize(20);
break;
case R.id.font_30:
text_main_info.setTextSize(30);
break;
case R.id.font_red:
text_main_info.setTextColor(Color.RED);
break;
case R.id.font_green:
text_main_info.setTextColor(Color.GREEN);
break;
}
return super.onOptionsItemSelected(item);
}
第二种:上下文菜单
上下文菜单的使用:
步骤:
1.为一个View关联一个上下文
registerForContextMenu(View)
2.实现创建上下文菜单的方法
CreateContextMenu();
public classMainActivity extends Activity {
private TextView tv;
3.实现点击事件的选择器
onContextItemSelected();
===============================================
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
1.为textView关联一个上下文
registerForContextMenu(tv);
}
2.实现创建上下文菜单的方法
@Override
public void onCreateContextMenu(ContextMenu menu,View v,
ContextMenuInfomenuInfo) {
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("修改信息");
menu.add(0, 1, 0, "修改颜色");
menu.add(0, 2, 1, "修改字体");
menu.add(0, 3, 2, "删除");
}
3.实现点击的选择器
@Override
onContextItemSelected(MenuItemitem) {
// TODO Auto-generated methodstub
switch (item.getItemId()) {
case 1:
tv.setTextColor(Color.RED);
break;
case 2:
tv.setTextSize(20);
break;
case 3:
tv.setText("");
break;
default:
break;
}
returnsuper.onContextItemSelected(item);
}
}
综合应用:
布局:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView_main_info"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
子布局:
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入新的名字"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</LinearLayout>
逻辑代码:
public classMainActivity extends Activity {
private ListView lv;
private String[] arrName = new String[]{ "唐僧", "悟空", "猪八戒", "沙和尚" };
private ArrayAdapter<String>adapter = null;
private List<String> list =null;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv);
list=newArrayList<String>();
//1.数据源
for (int i = 0; i <arrName.length; i++) {
list.add(arrName[i]);
}
2.构建适配器
adapter=newArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1, list);
绑定
lv.setAdapter(adapter);
注册上下文菜单
registerForContextMenu(lv);
}
5.创造上下文菜单
@Override
onCreateContextMenu(ContextMenumenu, View v,
ContextMenuInfomenuInfo) {
// TODO Auto-generated methodstub
//设置基本信息
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("西游记");
//添加子菜单
menu.add(0, 1, 0, "修改名字");
menu.add(0, 2, 1, "删除名字");
menu.add(0, 3, 2, "取消");
super.onCreateContextMenu(menu,v, menuInfo);
}
6.响应点击事件
@Override
onContextItemSelected(MenuItem item) {
// TODO Auto-generated methodstub
//难点,重点
final AdapterContextMenuInfoinfo=(AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case 1:
//创AlerDialog的构建器
AlertDialog.Builderbuilder=new AlertDialog.Builder(this);
//设置基本信息
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("修改信息");
ViewdialogView=getLayoutInflater().inflate(R.layout.alertdialog_item, null);
final EditTextet=(EditText) dialogView.findViewById(R.id.editText_name);
添加布局
builder.setView(dialogView);
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which) {
//TODO Auto-generated method stub
Stringname=et.getText().toString().trim();
list.set(info.position,name);
//因为数据是和适配器绑定的所以只要更新适配器就可以了
adapter.notifyDataSetChanged();
}
});
builder.create().show();
break;
case 2:
list.remove(info.position);
adapter.notifyDataSetChanged();
break;
default:
break;
}
returnsuper.onContextItemSelected(item);
}
}
第三种:
PopuWindow的使用:弹出性窗口
packagecom.jason.menu;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.drm.DrmStore.Action;
importandroid.view.Gravity;
importandroid.view.KeyEvent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
importandroid.widget.ImageView;
importandroid.widget.PopupWindow;
public classMainActivity extends Activity {
private PopupWindow popupWindow=null;
private View contentView=null;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentView= getLayoutInflater().inflate(
R.layout.popuwindow_item,null);
// 1.创建一个弹出式窗口
popupWindow = newPopupWindow(contentView,
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
// 2.
ImageView imageView_back =(ImageView) contentView
.findViewById(R.id.imageView1);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(Viewv) {
// TODOAuto-generated method stub
}
});
ImageView imageView_share =(ImageView) contentView
.findViewById(R.id.imageView2);
imageView_share.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
// TODOAuto-generated method stub
}
});
ImageView imageView_conlect =(ImageView) contentView
.findViewById(R.id.imageView3);
imageView_conlect.setOnClickListener(newOnClickListener() {
@Override
public void onClick(Viewv) {
Intentintent =new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:13822101945"));
intent.putExtra("sms_body","hello!");
startActivity(intent);
}
});
}
@Override
//专门用来获取菜单按钮和回退按钮的触发器
public boolean onKeyDown(int keyCode,KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(popupWindow.isShowing()){
popupWindow.dismiss();
}
break;
case KeyEvent.KEYCODE_MENU:
if(popupWindow.isShowing()){
popupWindow.dismiss();
}else{
popupWindow.showAtLocation(contentView,Gravity.BOTTOM, 0, 0);
}
break;
default:
break;
}
returnsuper.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menumenu) {
// Inflate the menu; thisadds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return false;
}
}
掌握AlertDialog的使用:
分类:
1. AlertDialog
2. ProgressDialog 进度对话框
3. DatePickerDialog 日期选择对话框
4. TimePickerDialog
5. 自定义对话框(登录对话框、关于对话框)
6. 列表对话框
• 普通列表对话框
• 单选列表对话框
• 多选列表对话框
• 带图标的列表对话框
第一种:AlertDialog(提示对话框)
AlertDialog.Builderbuilder = new AlertDialog.Builder(this);
// 设置builder的属性
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("提示!");
AlertDialog!");
// 创建Action Button
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你确定要退出吗?", 0).show();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你确定要取消吗?", 0).show();
}
});
builder.setNeutralButton("不操作", new OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which) {
}
});
// 创建一个AlertDialog
builder.create().show();
第二种:ProgressDialog 进度对话框
private ProgressDialog progressDialog;
progressDialog=new ProgressDialog(context);
progressDialog.setTitle("提示");
progressDialog.setMessage("正在下载,请稍候");
progressDialog.setIcon(R.drawable.ic_launcher);
//显示
progressDialog.show();
//关闭
progressDialog.dismiss();
第三种:单选的对话框
AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
builder4.setTitle("选择字体");
builder4.setIcon(R.drawable.ic_launcher);
// 1. 数据源
final String[] arrFont = new String[] { "10sp", "12sp", "14sp",
"16sp" };
// 2. 设置
builder4.setSingleChoiceItems(arrFont, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
position = which;
}
});
builder4.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (position) {
case 0:
Toast.makeText(MainActivity.this, "10sp", 0).show();
break;
case 1:
Toast.makeText(MainActivity.this, "12sp", 0).show();
break;
case 2:
Toast.makeText(MainActivity.this, "14sp", 0).show();
break;
case 3:
Toast.makeText(MainActivity.this, "16sp", 0).show();
break;
default:
break;
}
}
});
builder4.create().show();第四种:多选对话框
AlertDialog.Builder builder5 = new AlertDialog.Builder(this);
builder5.setTitle("选择兴趣爱好");
builder5.setIcon(R.drawable.ic_launcher);
// 1. 数据源
final String[] arrHobby = new String[] { "读书", "打球", "看电影", "睡觉" };
// map =new HashMap<Integer, Boolean>();
final boolean[] pos = new boolean[arrHobby.length];
// 2. 设置
builder5.setMultiChoiceItems(arrHobby, null,
new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
System.out.println(which);
Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
map.put(which, isChecked);
list.add(map);
// pos[which] =isChecked;
}
});
builder5.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// System.out.println(which);
// boolean state=map.get(which);
// if(state){
// Toast.makeText(MainActivity.this, "选择了:"+arrHobby[which],
// 0).show();
// }
// StringBuffer sb =new StringBuffer();
// sb.append("你勾选了:");
// for (int i = 0; i < pos.length; i++) {
// if(pos[i]){
// sb.append(arrHobby[i]);
// sb.append(",");
// }
// }
StringBuffer sb =new StringBuffer();
sb.append("你勾选了:");
Toast.makeText(MainActivity.this, sb.toString(), 0).show();
}
});
builder5.create().show();第五种:自定义对话框
// 1. 创建视图View
View dialogview = getLayoutInflater().inflate(R.layout.dialog_item,
null);
final TextView textView_main_name = (TextView) dialogview
.findViewById(R.id.editText_main_name);
final TextView textView_main_pwd = (TextView) dialogview
.findViewById(R.id.editText_main_pwd);
AlertDialog.Builder builder6 = new AlertDialog.Builder(this);
builder6.setTitle("登录信息");
builder6.setIcon(R.drawable.ic_launcher);
// 2. 为dialog绑定一个view
builder6.setView(dialogview);
// 3. 设置ActionButton
builder6.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = textView_main_name.getText().toString();
String pwd = textView_main_pwd.getText().toString();
Toast.makeText(MainActivity.this, name + ":" + pwd, 0)
.show();
}
});
builder6.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
textView_main_name.setText(" ");
textView_main_pwd.setText(" ");
}
});
// 4. 创建Dialog并显示
builder6.create().show();
通知的使用:
第一种:普通的通知
创建普通通知的步骤:
1. 定义一个NotificationManager管理器并且实例化
private NotificationManager
manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2. 构建通知的Builder
NotificationCompat.Builder builder=newNotificationCompat.Builder(this);
3. 设置必要属性
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("普通的通知");
builder.setContentText("通知来了");
4. 添加附件属性
//添加声音
builder.setSound(Uri.parse("android.resource://"+
getPackageName()+
"/"+R.raw.a));
//添加滚动
builder.setTicker("sjdlkjjjjjjjjjjjjjjjjjjjjjjjjsdjk;fjfjlsdkjfsdjflsdf"+
"dskjfkldsssdjflkkkkkkkkkkkkjfsldjfs"+
"d;skfjjjjjjjjjjjjjjjjjjf");
//点击时跳到另外一个界面
//public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
Intentintent=new Intent(MainActivity.this, Second.class);
PendingIntentpIntent=PendingIntent.getActivity(MainActivity.this,100, intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pIntent);
5. 用Builder创建一个通知对象
Notification notification=builder.build();
6. 发布通知
manager.notify(1,notification);
第二种 带进度条的通知
public class MainActivity extends Activity {
private NotificationManagermanager;
private intsize=0;
privateString urlString = "http://m2.qiushibaike.com/article/list/suggest?page=";
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
public voidclickButton(View view){
switch(view.getId()) {
caseR.id.button1:
newMyAsyncTask(this,manager).execute(urlString);
break;
default:
break;
}
}
public classMyAsyncTask extends AsyncTask<String, Integer,byte[]>{
privateContext context;
privateNotificationCompat.Builder builder=null;
privateNotificationManager manager=null;
publicMyAsyncTask(Context context,NotificationManager manager) {
this.context=context;
builder=newNotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("带进度条的通知");
builder.setContentText("通知来了!");
builder.setProgress(100,0,false);
this.manager=manager;
}
@Override
protectedvoid onPreExecute() {
manager.notify(1,builder.build());
}
@Override
protectedbyte[] doInBackground(String... params) {
//TODO Auto-generated method stub
InputStreamis=null;
ByteArrayOutputStreambos=null;
HttpURLConnectionconnection=null;
try{
URLurl=new URL(params[0]);
connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(2000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Accept-Encoding","identity");
connection.connect();
intsum=connection.getContentLength();
System.out.println("==================="+sum);
if(connection.getResponseCode()==200) {
is=connection.getInputStream();
byte[]b=newbyte[10];
intlen=-1;
bos=newByteArrayOutputStream();
while((len=is.read(b))!=-1) {
bos.write(b,0, len);
bos.flush();
size+=len;
publishProgress((int)(size/(float)sum*100));
}
}
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnbos.toByteArray();
}
@Override
protectedvoid onProgressUpdate(Integer... values) {
builder.setProgress(100,values[0], false);
manager.notify(1,builder.build());
}
@Override
protectedvoid onPostExecute(byte[] result) {
builder.setProgress(0,0, false);
manager.notify(1,builder.build());
}
}
}
第三种:大视图的通知
1. 定义一个NotificationManager管理器并且实例化
2. 构建通知的builder对象
3. 设置必须属性
4. 设置.setStyle方法
InboxStyle inboxStyle = new InboxStyle();
"Eventtracker details:");
for(int i = 0; i < 10; i++) {
inboxStyle.addLine("jason"+ i);
}
builder1.setStyle(inboxStyle);
5. 创建通知对象并发布
manager.notify(2, builder1.build());
android 二级列表 父item屏幕顶部 安卓二级菜单怎么设置
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【爬虫软件】快手评论区采集:自动采集1024w条,含二级评论、展开评论!
用python开发的快手评论采集软件工具。
python 爬虫 快手采集 采集快手评论区 爬取快手