android 常用控制項

來源:互聯網
上載者:User

1.文本控制項 TextView

跑馬燈效果:系統提供的textView文本雖然內建跑馬燈效果但是只有擷取到焦點的時候才能實現效果,

所以需要重寫TextView類的isFocused()方法使它永遠返回true

public class ScrollForeverTextView extends TextView {public ScrollForeverTextView(Context context) {super(context);// TODO Auto-generated constructor stub}public ScrollForeverTextView(Context context, AttributeSet attrs) {super(context, attrs);}public ScrollForeverTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic boolean isFocused() {return true;//關鍵是這個方法}}

加入xml中的代碼   ScrollForeverTextView前面的是這個類所在的包名 

 <com.quickeasytrip.view.ScrollForeverTextView                    android:id="@+id/book_hotel_name_verify"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:ellipsize="marquee"                    android:singleLine="true"                    android:marqueeRepeatLimit="marquee_forever"                    android:scrollHorizontally="true"                     />

2.EditView 編輯框

   剛開啟Activity的時候,很可能由於布局中含有一個EditView控制項而自動彈出一個IME,要想去掉這個討厭的IME框只要在EditView的父布局中加入一個屬性即可

  解決之道:在EditText的父級控制項中找一個,設定          android:focusable="true"           android:focusableInTouchMode="true"      這樣,就把EditText預設的行為截斷了!

  點編輯框輸入使用者名稱密碼時,最經常遇到的問題就是,IME把編輯框遮擋,如果想編輯下一個EditView就必須關閉IME, 

  UI布局也與IME分離,同時EditText地區自動縮小了。這樣的話,即不影響使用者輸入,也不影響使用者進一步操作!

  而且即使開啟了IME,使用者也可以看到UI全貌,感覺比較舒服、友好

                此時只要在AndroidManifest.xml檔案 這個activity中加入這個屬性就可以

        android:windowSoftInputMode="adjustResize"

                如果登入名稱稱是email類型,為方便使用者,設定android:inputType屬性就可以直接讓彈出的IME適應輸入郵件格式

 android:inputType="textEmailAddress"適合輸入郵件 android:inputType="number"適合輸入數字

  

3.ViewPage  
   使用這個控制項需要先添加這個包,你的工程---->右鍵   android tools -------->addSupportLibrary  添加jar包

   第一步 在xml裡添加控制項

   

 <android.support.v4.view.ViewPager  android:id="@+id/pagerMain"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_below="@+id/advert" />

   第二步  繼承PagerAdapter  之後ViewPager添加  這個Adapter即可

public class MyPagerAdapter extends PagerAdapter {public List<View> mListViews = null;ViewPager mViewPager = null;public MyPagerAdapter(List<View> mListViews, ViewPager viewPager) {this.mViewPager = viewPager;this.mListViews = mListViews;}@Overridepublic int getCount() {return mListViews.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == (arg1);}@Overridepublic Object instantiateItem(View container, int position) {((ViewPager) container).addView(mListViews.get(position));return mListViews.get(position);}@Overridepublic void destroyItem(View container, int position, Object object) {if (mListViews.size() > 0) {if ((mListViews.size() - 1) >= position) {mViewPager.removeView(mListViews.get(position));}//經過實際測試,這個方法經常會報下標越界錯誤,這樣寫能有效避免類似錯誤}}}

   4.提示框Dialog

      所有的提示類懸浮框都繼承自這個類Dialog。最常用的兩種提示框一個是AlertDialog,另一個是ProgresDialog,前者能和使用者互動接受使用者的操作,後者提示使用者         有 一個費時的操作進行中,請使用者耐心等待

      因為自己的應用程式很難和系統的主題相匹配,所以通常都會自己寫這兩類提示框。下面給出一個自訂ProgresDialog例子

      

   View pdView = factory.inflate(R.layout.pd_view, null);   Dialog mProgressDialog = new Dialog(this, R.style.nobgDialog);   mProgressDialog.setContentView(pdView);  //最後執行show方法顯示提示框mProgressDialog.show(); 

      pd_view的代碼

      

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="150dip"    android:layout_height="wrap_content"    android:background="@drawable/pd_bg"    android:gravity="center"    android:orientation="horizontal" >    <ProgressBar        android:id="@+id/loading_process"        android:layout_width="34dip"        android:layout_height="34dip"        android:indeterminate="false"        android:indeterminateDrawable="@anim/frame_animation" />    <TextView        android:id="@+id/mPdText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="10dip"        android:text="請稍後..."        android:textColor="@android:color/black"        android:textSize="14dip"        android:textStyle="bold" /></LinearLayout>

動畫列表代碼,當達到八幀每幀100毫秒的時候轉動效果最流暢

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item android:drawable="@drawable/scan_01" android:duration="100"/>    <item android:drawable="@drawable/scan_02" android:duration="100" />    <item android:drawable="@drawable/scan_03" android:duration="100" />    <item android:drawable="@drawable/scan_04" android:duration="100" />    <item android:drawable="@drawable/scan_05" android:duration="100" />    <item android:drawable="@drawable/scan_06" android:duration="100" />    <item android:drawable="@drawable/scan_07" android:duration="100" />    <item android:drawable="@drawable/scan_08" android:duration="100" /></animation-list>

activity提供了一個方法專門管理所有的提示框

@Overrideprotected Dialog onCreateDialog(int id, Bundle args) {ProgressDialog dialog = new ProgressDialog(this);dialog.setMessage("等待狂");return dialog;}

調用showDialog(id);來顯示具體的提示框,編程時dialog.show() 也能顯示,好像這個方法是沒用的,事實上,當你按back鍵返回操作時

前者會消除提示框,後者則不會有反應,需要自己重寫onKeyDown()方法來消除提示框

5 ContextMenu 操作功能表

   第一步  覆寫activity    onCreateContextMenu()方法

   

@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {    // set context menu title    menu.setHeaderTitle("檔案操作");    // add context menu item    menu.add(0, 1, Menu.NONE, "發送");    menu.add(0, 2, Menu.NONE, "標記為重要");    menu.add(0, 3, Menu.NONE, "重新命名");    menu.add(0, 4, Menu.NONE, "刪除");super.onCreateContextMenu(menu, v, menuInfo);}

    第二步  給操作功能表註冊一個VIew,當長按View的時候快顯功能表

    registerForContextMenu(View);

   

   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.