007.androidUI开发进阶(基础--案例) .
1.Dialog有四种,分别是AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog
1.1AlertDialog
public class MainActivity extends Activity { private View view; private TextView tView; private EditText eText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.cell, null); tView = (TextView) view.findViewById(R.id.tv2); eText = (EditText) view.findViewById(R.id.et1); new AlertDialog.Builder(this).setTitle("友情提示").setView(view) .setMessage("确定退出?") .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show(); } }).setNeutralButton("中立", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show(); } }).setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, eText.getText() .toString(), Toast.LENGTH_SHORT).show(); // tView.setText(eText.getText().toString()); } }).show(); } }
1.2ProgressDialog
ProgressDialog pgd = new ProgressDialog(this); pgd.setTitle("nihao"); pgd.setMessage("hehe"); //pgd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pgd.setProgress(100); pgd.show();
1.3DatePickerDialog
DatePickerDialog datePickerDialog = new DatePickerDialog(this,new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub } }, myear,mmouth,mday);datePickerDialog.show();
1.4 TimePickerDialog
于1.3相同的使用方式。
2.Menu有三种:分别是:optionsMenu,SubMenu,ContextMenu
2.1.OptionsMenu
public boolean onCreateOptionsMenu(Menu menu) { menu.add(menu.NONE,1,menu.NONE,"菜单1"); menu.add(menu.NONE,2,menu.NONE,"菜单2"); menu.add(menu.NONE,3,menu.NONE,"菜单3"); menu.add(menu.NONE,4,menu.NONE,"菜单4"); menu.add(menu.NONE,5,menu.NONE,"菜单5"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this,"1", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(this,"其他", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
2.2.SubMenu
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(menu.NONE,1,menu.NONE,"菜单1"); menu.add(menu.NONE,2,menu.NONE,"菜单2"); menu.add(menu.NONE,3,menu.NONE,"菜单3"); menu.add(menu.NONE,4,menu.NONE,"菜单4"); menu.add(menu.NONE,5,menu.NONE,"菜单5"); SubMenu subMenu = menu.addSubMenu(menu.NONE,6,menu.NONE,"菜单6"); subMenu.setHeaderTitle("你好"); subMenu.setIcon(R.drawable.ic_launcher); subMenu.addSubMenu(menu.NONE,7,menu.NONE,"菜单7"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this,"1", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(this,"其他", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
2.3ContextMenu
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.tv1); registerForContextMenu(textView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(menu.NONE,1,menu.NONE,"菜单1"); menu.add(menu.NONE,2,menu.NONE,"菜单2"); menu.add(menu.NONE,3,menu.NONE,"菜单3"); menu.add(menu.NONE,4,menu.NONE,"菜单4"); menu.add(menu.NONE,5,menu.NONE,"菜单5"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 1: Toast.makeText(this,"1",Toast.LENGTH_SHORT).show(); break; default: break; } return super.onContextItemSelected(item); } }
3.Notification
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //声明Notification Notification notification = new Notification(); //设置notification的参数 notification.icon=R.drawable.ic_launcher; Intent intent = new Intent(this,MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent,0); notification.setLatestEventInfo(this,"haha" ,"contentText",pendingIntent); notificationManager.notify(10,notification);
4.TabHost
TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(), true); Intent intent = new Intent(MainActivity.this, SeocndActivity.class); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("搜索") .setContent(intent)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("搜索") .setContent(intent)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("搜索") .setContent(R.id.l1));