android3.0新特性UI

时间:2022-12-18 18:57:31


Android3.0新增UI控件


AdapterViewAnimator控件


AdapterViewAnimator(堆View),已知子类有AdapterViewFlipper,StackView,是一系列View的集合,这些View叠加一起,并且View之间可以进行切换,并且在多个View切换过程体现渐隐渐现的动画效果。通过AdapterViewAnimator.setAdapter()方法设置一组要显示的View,通过AdapterViewAnimator.showPrevious()方法移动到前一个View,通过AdapterViewAnimator.showNext()移动到下一个View,示例代码如下:


效果图:

Src/com.lenovo.cn/AdapterViewAnimatorActivity.java

res/layout/adapterviewanimator_layout.xml

AdapterViewFlipper控件


AdapterViewFilpper(堆View),是AdapterViewAnimator的子类,是一系列View的集合,这些View叠加一起,并且View之间可以进行切换,并且在多个View切换过程体现渐隐渐现的动画效果。通过AdapterViewFilpper.setAdapter()方法设置一组要显示的View,通过AdapterViewFilpper.showPrevious()方法移动到前一个View,通过AdapterViewFilpper.showNext()移动到下一个View,示例代码如下:


效果图:

Src/com.lenovo.cn/AdapterViewFlipperActivity.java

package com.lenovo.cn;

import android.app.Activity;

import android.graphics.Color;  

import android.os.Bundle;  

import android.view.View;  

importandroid.view.View.OnClickListener;  

import android.widget.AdapterViewFlipper;

import android.widget.Button;  

public classAdapterViewFlipperActivity extendsActivity {

    private int [] mColors = {Color.BLUE,Color.CYAN, Color.GRAY, Color.GREEN, Color.RED};

   @Override 

   public void onCreate(Bundle savedInstanceState) {  

       super.onCreate(savedInstanceState);  

       setContentView(R.layout.adapterviewflipper_layout);  

       final AdapterViewFlipperadapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.mAdapterViewFlipper);  

       ColorAdapter colorAdapter = newColorAdapter(this, mColors);

       adapterViewFlipper.setAdapter(colorAdapter);  

       final Button previousButon =(Button) findViewById(R.id.previousButton);  

       previousButon.setOnClickListener(newOnClickListener() {  

            public void onClick(Viewview) {  

               adapterViewFlipper.showPrevious();  

            }  

       });  

       final Button nextButton =(Button) findViewById(R.id.nextButton);  

       nextButton.setOnClickListener(newOnClickListener() {  

            public void onClick(Viewview) {  

               adapterViewFlipper.showNext();  

            }  

       });  

   }

}

res/layout/adapterviewflipper_layout.xml

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 

   android:orientation = "horizontal" 

   android:layout_width = "fill_parent" 

   android:layout_height = "fill_parent" 

   >  

   <AdapterViewFlipper  

       android:id = "@+id/mAdapterViewFlipper" 

       android:layout_width = "fill_parent"   

       android:layout_height = "wrap_content"   

       android:loopViews = "true" 

       />  

   <LinearLayout  

       android:orientation = "vertical" 

       android:layout_width = "wrap_content" 

       android:layout_height = "wrap_content" 

       >  

       <Button  

            android:id = "@+id/previousButton" 

            android:layout_width = "wrap_content" 

            android:layout_height = "wrap_content" 

           android:text = "@string/before" 

            />  

       <Button  

            android:id = "@+id/nextButton" 

            android:layout_width = "wrap_content" 

            android:layout_height = "wrap_content" 

            android:text = "@string/next" 

            />  

   </LinearLayout>  

</LinearLayout>

 

ListPopupWindow控件

  锚于宿主视图,并显示如一个建议,当输入到一个列表中的选项的EditText查看列表。

 

 

CalendarView控件

CalendarView是一个日历控件,作为显示和选择日期。日期范围的及日历样式是可配置的。用户可以选择一个日期,也可以触摸滚动日历。可在xml布局中添加此控件,可通过CalendarView.setOnDateChangeListener()方法为此控件的每一格设置点击事件。示例代码如下:
效果图:

Src/com.lenovo.cn/CalendarViewActivity.java

package com.lenovo.cn;

import android.app.Activity;

import android.os.Bundle;

importandroid.widget.CalendarView;

importandroid.widget.CalendarView.OnDateChangeListener;

import android.widget.Toast;

publicclass CalendarViewActivityextends Activity{

    @Override

    protectedvoidsavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.calendarview_layout);

        CalendarViewcalendarView = (CalendarView) findViewById(R.id.calendarView);

        calendarView.setOnDateChangeListener(new OnDateChangeListener() {

            @Override

            publicvoid onSelectedDayChange(CalendarView view,int year,int month,int dayOfMonth) {

                Toast.makeText(CalendarViewActivity.this, getString(R.string.select) + year+getString(R.string.year) + month +getString(R.string.month) + dayOfMonth +getString(R.string.day), Toast.LENGTH_SHORT).show();

   

   

    }

}
res/layout/calendarview_layout.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"android:orientation="vertical"

android:layout_height="fill_parent">

    <CalendarViewandroid:layout_width="700dip"

android:layout_height="750dip"

android:id="@+id/calendarView"

/>

</LinearLayout>

 

NumberPicker控件

  这控件可用于设置时间的控件,允许用户选择一个数字,从一个预定范围。该部件提供了一个输入字段和向上和向下选择一个数字按钮。可按这两个按钮选择一个值填入显示框中,显示框还允许用户触摸滚动,这时会显示上个与下个的值,可以选择当前值来显示。此类中提供了NumberPicker.setLeft()方法设置最小值,NumberPicker.setMaxValue()设置最大值,可用NumberPicker.setOnValueChangedListener()方法监听更改值事件,只需new OnValueChangeListener对象,并重写里面onValueChange()方法即可,示例代码如下:

效果图:

Src/com.lenovo.cn/NumberPickerActivity.java

package com.lenovo.cn;

import android.app.Activity;

import android.os.Bundle;

import android.widget.NumberPicker;

importandroid.widget.NumberPicker.OnValueChangeListener;

import android.widget.Toast;

public classNumberPickerActivity extendsActivity {

    NumberPickermNumberPicker1 = null;

    NumberPickermNumberPicker2 = null;

    NumberPickermNumberPicker3 = null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.numberpicker_layout);

        mNumberPicker1 = (NumberPicker)findViewById(R.id.numberPicker1);

        mNumberPicker1.setLeft(0);

        mNumberPicker1.setMaxValue(24);

        mNumberPicker1.setOnValueChangedListener(new OnValueChangeListener() {

            @Override

            public void onValueChange(NumberPicker picker, int oldVal, int newVal){

                Toast.makeText(NumberPickerActivity.this,picker.getValue()+getString(R.string.hours), Toast.LENGTH_SHORT).show();

            }

        });

        mNumberPicker2 = (NumberPicker)findViewById(R.id.numberPicker2);

        mNumberPicker2.setLeft(0);

        mNumberPicker2.setMaxValue(60);

        mNumberPicker2.setOnValueChangedListener(new OnValueChangeListener() {

            @Override

            public void onValueChange(NumberPicker picker, int oldVal, int newVal){

                Toast.makeText(NumberPickerActivity.this,picker.getValue()+getString(R.string.minutes), Toast.LENGTH_SHORT).show();

            }

        });

        mNumberPicker3 = (NumberPicker)findViewById(R.id.numberPicker3);

        mNumberPicker3.setLeft(0);

        mNumberPicker3.setMaxValue(60);

        mNumberPicker3.setOnValueChangedListener(new OnValueChangeListener() {

            @Override

            public void onValueChange(NumberPicker picker, int oldVal, int newVal){

                Toast.makeText(NumberPickerActivity.this,picker.getValue()+getString(R.string.second), Toast.LENGTH_SHORT).show();

            }

        });

    }

}

res/layout/numberpicker_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

 xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:orientation="vertical">

 <TextView android:text="@string/hourset"

       android:layout_width="80dip"

       android:layout_height="wrap_content"

       android:gravity="center_horizontal"/>

 <LinearLayout

         android:layout_width="fill_parent"

         android:layout_height="wrap_content"

         android:orientation="horizontal">

         <NumberPicker android:id="@+id/numberPicker1"

               android:layout_width="80dip"

               android:layout_height="wrap_content"

               android:focusable="true"

               android:focusableInTouchMode="true"/>

          <TextView android:text="@string/hours"

                android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

         <NumberPicker android:id="@+id/numberPicker2"

               android:layout_width="80dip"

               android:layout_height="wrap_content"

               android:focusable="true"

               android:focusableInTouchMode="true"/>

          <TextView android:text="@string/minutes"

                android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

         <NumberPicker android:id="@+id/numberPicker3"

               android:layout_width="80dip"

               android:layout_height="wrap_content"

               android:focusable="true"

               android:focusableInTouchMode="true"/>

          <TextView android:text="@string/second"

                android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

 

StackView控件


StackView(堆View),是AdapterViewAnimator的子类,一系列View的集合,这些View以层叠样式显示,并且View之间可以进行切换(切换方式有两种:(1)拖动StackView组件中的某一个View,(2)通过代码控制),这样每一个View都有机会显示给用户,供用户使用,并且在多个View切换过程中可以添加动画效果。通过StackView.setAdapter()方法设置一组要显示的View,通过StackView.showPrevious()方法移动到前一个View,通过StackView.showNext()移动到下一个View示例代码如下:


效果图:



Src/com.lenovo.cn/StackViewActivity.java

package com.lenovo.cn;

import android.app.Activity;

import android.graphics.Color;  

import android.os.Bundle;  

import android.view.View;  

importandroid.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.StackView;

public classStackViewActivity extends Activity {

    private int [] mColors = {Color.BLUE,Color.CYAN, Color.GRAY, Color.GREEN, Color.RED};

   @Override 

   public void onCreate(Bundle savedInstanceState) {  

       super.onCreate(savedInstanceState);  

       setContentView(R.layout.stackview_layout);  

       final StackView stackView = (StackView) findViewById(R.id.mStackView);  

       ColorAdapter colorAdapter = newColorAdapter(this, mColors);  

       stackView.setAdapter(colorAdapter);  

       final Button previousButon =(Button) findViewById(R.id.previousButton);  

       previousButon.setOnClickListener(newOnClickListener() {  

            public void onClick(Viewview) {  

                stackView.showPrevious();  

            }  

       });  

       final Button nextButton =(Button) findViewById(R.id.nextButton);  

       nextButton.setOnClickListener(newOnClickListener() {  

            public void onClick(Viewview) {  

                stackView.showNext();  

            }  

       });  

   }

}

Src/com.lenovo.cn/ColorAdapter.java

package com.lenovo.cn;

import android.content.Context;  

import android.view.View;  

import android.view.ViewGroup;  

import android.widget.BaseAdapter;  

import android.widget.LinearLayout;  

public classColorAdapter extends BaseAdapter {  

   private ContextmContext;  

   private int [] mColors;  

   public ColorAdapter(Contextcontext, int [] colors) {  

       mContext = context;  

       mColors = colors;  

   }  

   public int getCount() {  

       return mColors == null ? 0 : mColors.length;  

   }  

   public Object getItem(int position) {  

       return mColors == null ?null : mColors[position];  

   }  

   public long getItemId(intposition) {  

       return position;  

   }  

   public View getView(int position, View cacheView, ViewGroupparent) {  

       LinearLayout.LayoutParams colorLayoutParams = new LinearLayout.LayoutParams(100, 100);  

       LinearLayout colorLayout = newLinearLayout(mContext);  

       colorLayout.setBackgroundColor(mColors[position]);  

       colorLayout.setLayoutParams(colorLayoutParams);  

       return colorLayout;  

   }  

}

res/layout/stackview_layout.xml

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 

   android:orientation = "horizontal" 

   android:layout_width = "fill_parent" 

   android:layout_height = "fill_parent" 

   >  

   <StackView  

       android:id = "@+id/mStackView" 

       android:layout_width = "fill_parent"   

       android:layout_height = "wrap_content"   

       android:loopViews = "true" 

       />  

   <LinearLayout  

       android:orientation = "vertical" 

       android:layout_width = "wrap_content" 

       android:layout_height = "wrap_content" 

       >  

       <Button  

            android:id = "@+id/previousButton" 

            android:layout_width = "wrap_content" 

            android:layout_height = "wrap_content" 

            android:text = "@string/before"  

           />  

       <Button  

            android:id = "@+id/nextButton" 

            android:layout_width = "wrap_content" 

            android:layout_height = "wrap_content" 

            android:text = "@string/next" 

            />  

   </LinearLayout>  

</LinearLayout>

 

PopupMenu控件

  PopupMenu是个下拉菜单控件,可增加多条下拉菜单以及给每条下拉菜单增加子菜单。

可通过new PopupMenu(context, view)创建一个下拉菜单,view为点击的控件,点此view时显示菜单,通过PopupMenu.getMenuInflater().inflate(R.menu.popup, PopupMenu.getMenu())方式为下接菜单设置主菜单项及子菜单项,R.menu.popup为下拉菜单资源文件,getMenu用于取得与此相关的弹出菜单对象,整个方法的作用就是将资源文件中的菜单项加入弹出菜单中,通过PopupMenu.setOnMenuItemClickListener()方法为每个弹出菜单项设定点击事件。示例代码如下:

效果图:

Src/com.lenovo.cn/PopupMenuActivity.java

package com.lenovo.cn;

import android.app.Activity;

import android.os.Bundle;

import android.view.MenuItem;

import android.view.View;

import android.widget.PopupMenu;

import android.widget.Toast;

public classPopupMenuActivity extends Activity {

    PopupMenupopup = null;

   @Override

   protected void onCreate(Bundle savedInstanceState){

       super.onCreate(savedInstanceState);

       setContentView(R.layout.popupmenu_layout);

   }

   public void onPopupButtonClick(View button) {

       popup = new PopupMenu(this, button);

       popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

       popup.setOnMenuItemClickListener(newPopupMenu.OnMenuItemClickListener() {

            public booleanonMenuItemClick(MenuItem item) {

               if(item.getTitle().equals(getString(R.string.exit))){

                    popup.dismiss();

                }else{

                    Toast.makeText(PopupMenuActivity.this, getString(R.string.click) + item.getTitle(),Toast.LENGTH_SHORT).show();

                }

                return true;

            }

       });

       popup.show();

   }

}

res/menu/popup.xml

<?xml version="1.0"encoding="utf-8"?>

<menuxmlns:android="http://schemas.android.com/apk/res/android">

   <item android:id="@+id/search"

         android:icon="@android:drawable/ic_menu_search"

          android:title="@string/popup_menu_search"/>

   <item android:id="@+id/add"

         android:icon="@android:drawable/ic_menu_add"

         android:title="@string/popup_menu_add" />

   <item android:id="@+id/edit"

         android:icon="@android:drawable/ic_menu_edit"

         android:title="@string/popup_menu_edit">

       <menu>

            <itemandroid:id="@+id/share"

                 android:icon="@android:drawable/ic_menu_share"

                 android:title="@string/popup_menu_share" />

       </menu>

   </item>

    <item android:id="@+id/edit"

         android:icon="@android:drawable/ic_menu_edit"

         android:title="@string/popup_menu_edit"/>

</menu>

res/layout/popupmenu_layout.xml

<?xml version="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">

   <Button android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           android:layout_gravity="center"

           android:onClick="onPopupButtonClick"

           android:text="@string/popup_menu_button" />

</LinearLayout>

 

SearchView控件

  SearchView是一个搜索框控件,可增加一个ListView实现自动补全功能,通过Adapter给此控件加入自动补全列表,可根据输入的字段进行搜索,SearchView.setOnQueryTextListener()监听事件,其中onQueryTextChange()方法监听输入事件,onQueryTextSubmit()方法监听回车或搜索按钮事件。SearchView.setSubmitButtonEnabled()方法用于显示一个执行搜索功能的按钮,点此按钮时会触发onQueryTextSubmit()方法。SearchView.setQueryHint()方法用于控件默认显示的文本,示例代码如下:

效果图:

Src/com.lenovo.cn/SearchViewActivity.java

package com.lenovo.cn;

import android.app.Activity;

import android.os.Bundle;

import android.text.TextUtils;

import android.view.Window;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.SearchView;

import android.widget.Toast;

/**

 * 搜索框控件,可自动补全

 */

public classSearchViewActivity extends ActivityimplementsSearchView.OnQueryTextListener {

   private SearchViewmSearchView;

   private ListView mListView;

   /**自动补全列表**/

   private final String[] mStrings = {"a","b","c"};

   @Override

   protected void onCreate(Bundle savedInstanceState){

       super.onCreate(savedInstanceState);

       getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

       setContentView(R.layout.searchview_layout);

       mListView = (ListView) findViewById(R.id.list_view);

       mListView.setAdapter(newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));

       mListView.setTextFilterEnabled(true);

       mSearchView = (SearchView) findViewById(R.id.search_view);

       mSearchView.setIconifiedByDefault(false);

       mSearchView.setOnQueryTextListener(this);

       mSearchView.setSubmitButtonEnabled(true);       mSearchView.setQueryHint(getString(R.string.search));

}

 

   /**输入字符时触发**/

   public boolean onQueryTextChange(String newText) {

       if (TextUtils.isEmpty(newText)) {

            mListView.clearTextFilter();

       } else {

           mListView.setFilterText(newText.toString());

       }

       return true;

   }

 

   /**按回车或搜索按钮时触发,此处编写搜索代码**/

   public boolean onQueryTextSubmit(String query) {

       Toast.makeText(this,getString(R.string.search_click)+" "+ query, 0).show();

       return false;

   }

}

res/layout/searchview_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:orientation="vertical">

   <SearchView

            android:id="@+id/search_view"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"/>

   <ListView

            android:id="@+id/list_view"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_weight="1"/>

</LinearLayout>

 

Notification控件
  Notification扩展了一些功能,以便支持更多的内容来丰富的状态栏通知,增加了新Notification.Builder类允许您轻松地创建通知的对象。通过new Notification.Builder()创建一个Notification.Builder对象,builder.setDefaults()方法设置通知音乐,builder.setAutoCancel()方法设置点通知后,状态栏的通知自动隐去,builder.setContentTitle()方法设置通知标题,builder.setContentText()方法设置通知内容,现在Notification支持在通知时使用大图标了,可使用builder.setLargeIcon()方法设置,如果想继续使用小图标,可用builder.setSmallIcon()方法设置。可使用builder.setContentIntent()方法为通知设置点击后执行的事件。builder.getNotification()方法可获得Notification对象,示例代码如下:

Src/com.lenovo.cn/Notification_demo.java

package com.lenovo.cn;

import java.io.InputStream;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.graphics.Bitmap;

importandroid.graphics.drawable.BitmapDrawable;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public classNotification_demo extends Activity {

    NotificationManagermanager = null;

    Notification.Builderbuilder = null;

    Notificationn = null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.notification_layout);

        Button notification_click = (Button)findViewById(R.id.notification_click);

        manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        builder = new Notification.Builder(Notification_demo.this);

        builder.setDefaults(Notification.DEFAULT_SOUND);

        builder.setAutoCancel(true);

        builder.setContentTitle("title");

        builder.setContentText("message");

        InputStream is =getResources().openRawResource(R.drawable.abc);

        Bitmap bitmap = new BitmapDrawable(is).getBitmap();

        builder.setLargeIcon(bitmap);//设置了但是没效果

//      builder.setSmallIcon(R.drawable.icon);

        Intent intent = new Intent(this,PopupMenuActivity.class);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        builder.setContentIntent(pIntent);

       

        n = builder.getNotification();

        notification_click.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                manager.notify(0, n);

            }

        });

    }

 

}

res/layout/searchview_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:layout_width="match_parent"

              android:layout_height="match_parent"

              android:orientation="vertical"

              android:id="@+id/line">

   <Button android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_gravity="center"

            android:id="@+id/notification_click"

            android:text="@string/notification_click" />

</LinearLayout>