Android Day12-Fragment+Menu+AutoCompleteTextView+Notificationt

简介:

一、Fragment

    fragment,英文的意思是片断。

   

   Fragment是Android3.0(API11)出现的新特性,如果不采用兼容性的写法,新建工程的时候最小兼

   容版本的API应该为11以上。


    在Android中,Fragment代表一个行为或者是Activity中用户界面的一部分。可以将其理解为

    Activity模块化的片断。可以由多个Fragment组成activity,或者一个Fragment可以被多个

    Activity所共用。

   1)Fragment的生命周期

    Fragment有自己的生命周期,除了onRestart方法之后,Activity有的生命周期方法它全有。

    而且Fragment的生命周期方法也是两两呼应的。

    wKioL1W9jFOTTGOdAAF9NEvjCoc277.jpg 

     

     虽然有这么多的生命周期方法,在实际开发中经常用的只有2种:onCreateView和onDestroy

    ■onCreateView必需是要复写的,因为它要返回View对象。

    ■onDestroy方法中,主要是来做一些擦屁股的操作。

   2)Fragment怎么添加到Activity当中去

    2-1:在layout中定义Fragment的XML布局文件  

    2-2:定义Fragment中的用户类,复写onCreateView方法。

    2-3:将Fragment添加到Activity中,有2种方式。

    

    实现步骤:

    2-1:在layout中定义Fragment的XML布局文件 

    2-2:定义Fragment中的用户类,复写onCreateView方法。

      wKiom1W9juGwZ7w9AADZ4xC3JLM032.jpg 

    2-3:添加Fragment到Activity当中

      ■第1种方式:在Activity布局文件里添加Fragment,通过fragment标签(首字母小写)

            通过name属性来关联Fragment的用户类

       wKiom1W9j7GTu0DrAAE81dz7asQ912.jpg 

      ■第2种方式:通过Java代码来实现

1
2
3
4
5
6
7
8
9
            //第1步:得到FragmentManager对象
                FragmentManager fragmentManager = getFragmentManager();
            //第2步:开启事务,并返回Fragment事物。
             FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            //第3步:用事物为ViewGroup对象添加Fragment或者将ViewGroup替换成Fragment。
                 ExampleFragment fragment =  new  ExampleFragment();
                 fragmentTransaction.add(R.id.fragment_container, fragment);  //或者replace
            //第4步:提交事务     
                 fragmentTransaction.commit();

      

      兼容低版本的写法: 

        (1)定义的fragment必须使用v4包中的fragment

         (2)在获取fragment的管理者的activity必须继承FragmentActivity 

         (3)获取Fragment的管理者的方式和之前不一样,其它的代码都一样。

    3)Fragment的通讯

     假如Activity中有2两个Fragment,LeftFragment和RightFragment。假如LeftFragment有

     一个Button按钮,点击它改变RightFragment中的TextView的内容。应该做2件事情:

      ●第一步:在Activity中添加要被另外一个Fragment对象操作的Fragment时,为其指定一个

           tag。

1
                 transaction.replace(R.id.ll_right,  new  RightFragment(), "right" );

       ●第二步:在作为调用者的Button事件中,利用Activity这个桥梁找到要操作的Fragment。

1
2
                 RightFragment findFragmentRight = (RightFragment) getActivity().getFragmentManager().findFragmentByTag( "right" );
                 findFragmentRight.setText( "我被左边的Fragment操作了!" );



二、菜单(menu)

  1.创建菜单,要复写onCreateOptionsMenu方法

    创建菜单的方式有2种

    ●第1种:

      在res/menu下创建菜单的XML文件,格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
         < menu  xmlns:android = "http://schemas.android.com/apk/res/android"  >        
             <!-- orderInCategory这个属性决定了item的显示的前后位置 -->
             < item
                 android:id = "@+id/item_1"
                 android:orderInCategory = "10"   
                 android:title = "选项1" />
             
             < item
                 android:id = "@+id/item_2"
                 android:orderInCategory = "5"
                 android:title = "选项2" />
         </ menu >

      在onCreateOptionsMenu中,得到菜单的打气筒inflater来将布局填充到menu。

1
          getMenuInflater().inflate(R.menu.main, menu);

     ●第2种:直接用代码来动态的创建菜单

     4个参数的意义

        int groupId, int itemId, int order, CharSequence title

        分组、id、显示顺序、标题,其中id就是在onOptionsItemSelected里使用。

1
2
          menu.addSubMenu( 0 1 5 "选项111111" );  //各个参数与xml中的属性是一一对应的。
          menu.addSubMenu( 0 2 3 "选项2222222" );

   2.想找到具体点击的是哪个菜单的item,要实现onOptionsItemSelected方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     @Override
     public  boolean  onOptionsItemSelected(MenuItem item) {
        /*Toast.makeText(this, item.getTitle(), 0).show();得到选项item的名称*/    
        /*Toast.makeText(this, item.getItemId()+"", 0).show();得到选项item的id*/
         System.out.println(item.getItemId());
         switch  (item.getItemId()) {
         case  R.id.item_1:
           Toast.makeText( this , item.getTitle() +  "--5555555" 0 ).show();
           break ;
         case  R.id.item_2:
           Toast.makeText( this , item.getTitle()+  "--10101010" , 0 ).show();
           break ;
         default :
            break ;
         }
         return  super .onOptionsItemSelected(item);
     }

   
三、AutoCompleteTextView

   这个控件就像是加强版的EditText,可以定义适配器,当用户输入数据,如果适配器中有匹配的

   数据就会显示到一个下拉的菜单中。用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
      public  class  CountriesActivity  extends  Activity {
          protected  void  onCreate(Bundle icicle) {
              super .onCreate(icicle);
              setContentView(R.layout.countries);
     
              ArrayAdapter<String> adapter =  new  ArrayAdapter<String>( this ,
                      android.R.layout.simple_dropdown_item_1line, COUNTRIES);
              AutoCompleteTextView textView = (AutoCompleteTextView)
                      findViewById(R.id.countries_list);
              textView.setAdapter(adapter);
          }
     
          private  static  final  String[] COUNTRIES =  new  String[] {
              "Belgium" "France" "Italy" "Germany" "Spain"
          };
      }


四、通知栏

      就是像短信发送到手机时,在屏幕上方提示的信息条。

    wKioL1W91NSwMl3_AAAdU2S1OWk720.jpg

    通知一般会有2种展现方式,上面这个就是一般的显示方式Normal View,它的UI构成如下:

    wKioL1W91a6RkkGXAAF9y9Rg9uE440.jpg

    通知栏不仅可以提示信息,还能给它添加点击事件.如点击短信通知,就会跳转到短信页面.

  1.通知栏高版本的写法

    关于版本的通知栏的写法,官方API说得十分清楚:

    wKioL1W92LHxI4tZAAIZuO8yNLo330.jpg 

    第一段是这么说的:

        在Notification.Builder对象里为Notification指定UI信息和动作,Noti    

     fication.Builder.build()得到Notification对象,通过NotificationManager.notify()方

      法来发送通知给系统。

       下面就按照这个思路来写Java代码 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
       //链式写法
         Notification notification =  new  NotificationCompat.Builder( this )
                 .setContentTitle( "小芳" )
                 .setSmallIcon(R.drawable.ic_launcher)
                 .setContentText( "晚上老地方见" ).build(); 
         
       //给通知栏设置一个点击事件,假如点击之后开始打电话。
         Intent intent =  new  Intent();
         intent.setAction(Intent.ACTION_CALL);
         intent.setData(Uri.parse( "tel:"  110 ));
         PendingIntent pendingIntent = PendingIntent.getActivity( this 10 , intent,Intent.FLAG_ACTIVITY_NEW_TASK);
         
       //这个设置点击事件的方法会覆盖之前的标题和内容的设置
         notification.setLatestEventInfo( this "小芳" "晚上吃个饭吧" , pendingIntent);
         
       //得到NotificationManager对象
         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         notificationManager.notify( 0 , notification);

  

   2.通知栏低版本的写法

     与高版本的写法不同,低版本的Notification是直接new出来的,不需要用Builder。

1
2
3
4
5
6
7
8
9
10
11
12
13
     //构造方法3个参数:小图标、小图标文字、时间
         Notification notification =  new  Notification(R.drawable.ic_launcher, "小芳" ,System.currentTimeMillis());
 
     //给通知栏设置一个点击事件
         Intent intent =  new  Intent();
         intent.setAction(Intent.ACTION_CALL);
         intent.setData(Uri.parse( "tel:"  110 ));
         PendingIntent pendingIntent = PendingIntent.getActivity( this 10 , intent,Intent.FLAG_ACTIVITY_NEW_TASK);
         notification.setLatestEventInfo( this "小芳" "晚上吃个饭吧" , pendingIntent);
         
     //得到NotificationManager对象
         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         notificationManager.notify( 0 , notification);

    



      本文转自屠夫章哥  51CTO博客,原文链接:http://blog.51cto.com/4259297/1680895,如需转载请自行联系原作者





相关文章
|
8月前
|
Android开发
Android 中使用RadioGroup+Fragment实现底部导航栏的功能
Android 中使用RadioGroup+Fragment实现底部导航栏的功能
75 0
|
11月前
|
XML Android开发 数据格式
Android 底部导航栏(一、BottomNavigationView+Menu+Fragment)
现在常用的App主页都会有一个底部导航栏,根据需求也使用过好几种方法进行实现,于是想着还是总结一下,今天还写一个简单的BottomNavigationView方法来实现这个功能
|
Android开发
android activity方式的dialog
android activity方式的dialog
150 0
android activity方式的dialog
|
Android开发
简单讲解Android Fragment(二)
将Frament添加到activity中,这个时候有两种情况可以实现,第一种是静态添加,第二种是动态添加。
148 0
|
Android开发 数据格式 XML
Android dialog Activity 使用
尖角 dialog 弹窗使用很方便,我们都知道 Activity 也能搞成 dialog 形式,更加方便,下面我们下一个 dialog 样式的Activity 吧: 1、在 value/style.
1698 0
|
Android开发 容器 数据格式
Android--自定义toolbar(Fragment)
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/79260181 public ...
1384 0
|
Android开发 数据格式 XML
|
Android开发 数据格式 XML