android之組件1

 

UI組件

第一:TextView

(1)屬性(只是今天學到的)

android:autoLink  

<TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:autoLink="web"   將文本中的URL換爲超鏈接

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

     <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/email"

        android:autoLink="email"/>    將文本中的email換爲超鏈接      <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/phoneNumber"

        android:autoLink="phone"/>    將文本中的電話號碼換爲超鏈接       <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/mapUrl"

        android:autoLink="map"/>        將文本中的街道地址轉換爲超鏈接

 

注:他還有幾個屬性值,android:none   不設置任何超鏈接

                      android:all    相當於web/email/phone/map

     這裏只有在同時設置text時才自動識別鏈接,後來輸入的無法自動識別。

效果如圖:

 

 

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.border_tv);

        TextView tv = (TextView)findViewById(R.id.tvHtml);

        String htmlStr = "<font size='30' color='#00FF22'>我</font>愛<b>你</b>"

        +"<br><a href='http://hi.csdn.net/space.php?do=home'>lyhz</a>";

        tv.setText(Html.fromHtml(htmlStr));

}

這也的效果是

注意:這也是一個超鏈接,不過只具外觀,是不能夠跳轉的。

 

(2)  點邊框的TextView

自定義邊框

public void onDraw(Canvas canvas){

       super.onDraw(canvas);

       //創建畫刷

       Paint paint = new Paint();

       //設置邊框的顏色

       paint.setColor(android.graphics.Color.GREEN);

       //開始畫

       canvas.drawLine(0,0,this.getWidth()-1,0,paint);

       canvas.drawLine(0, 0, 0,this.getHeight()-1,paint);

           canvas.drawLine(this.getWidth()-1, 0,this.getWidth()-1, this.getHeight()-1, paint);

       canvas.drawLine(0,this.getHeight()-1,this.getWidth()-1, this.getHeight()-1, paint);

}

效果如圖:

 

第二:EditText

(1)     爲EditText對象註冊onKeyListener事件,實現onKey()方法

public class TestActivityextends Activity implements OnKeyListener{

   Button btn = null;

   EditText et = null;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.onkey);

        findView();

        btn.setOnClickListener(new OnClickListener() {

   //爲從編輯文本變成的按鈕,寫一個監聽器,點擊按鈕變爲編輯文本

          public void onClick(View v) {

              et.setVisibility(View.VISIBLE);

              btn.setVisibility(View.GONE);

          }

       });

    }

    private void findView(){

       btn = (Button)findViewById(R.id.button);

       et = (EditText)findViewById(R.id.edit);

       et.setOnKeyListener(this);

    }

   public boolean onKey(View v,int keyCode, KeyEvent event) {

       //取鍵值

       if(keyCode == KeyEvent.KEYCODE_ENTER){

          btn.setText(et.getText());

          et.setVisibility(View.GONE);

          btn.setVisibility(View.VISIBLE);

       }

       return true;

   }

 

(2)     自動完成輸入內容的組件

AutoCompleteTextView

MultiCompleteTextView

public class AutoActivityextends Activity {

AutoCompleteTextView autoTx =null;

  MultiAutoCompleteTextView mautoTx =null;

protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     setContentView(R.layout.auto);

     findViews();

     String[] str = { "abc", "add", "axy", "aaaa", "bcd", "bbbd", "bdcc" };

     ArrayAdapter adapter = new ArrayAdapter(this,

            android.R.layout.simple_dropdown_item_1line, str);

     autoTx.setAdapter(adapter);

     //-------MulitAutoCompleteTextView

     mautoTx.setAdapter(adapter);

       mautoTx.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

  }

  private void findViews() {

     autoTx = (AutoCompleteTextView) findViewById(R.id.auto);

     mautoTx = (MultiAutoCompleteTextView) findViewById(R.id.mauto);

  }

效果如圖:

 

 

Android ArrayAdapter 詳解

本文主要講解ArrayAdapter的創建方法,我把ArrayAdapter分爲三種:簡單的、樣式豐富的但內容簡單的、內容豐富的。

默認的,ArrayAdapter期望接受的樣式文件裏只含有一個textview,然後它把接受到的數據toString後(即調用數據對象的toString方法)展示在textview裏。

一、簡單的。

這樣的列表的每一行都只有一行文字。

Java代碼

  1. // 當然listview 也可以是在layout裏寫好,然後findViewById()獲取出來,這樣的話後面就不需setContentView(listview);       
  2.         ListView listview = new ListView(this);   
  3.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);   
  4.         adapter.add("string1");   
  5.         adapter.add("haha");   
  6.         adapter.add("heihei");         
  7.         listview.setAdapter(adapter);   
  8.         setContentView(listview);    

上面代碼中,android.R.layout.simple_expandable_list_item_1是android裏已提供的樣式,我們也可換成自己的xml。但是需要注意的是這個xml文件僅能有一個textview。連Layout也不能有。否則會報錯:ArrayAdapter requires the resource ID to be a TextView

如layout下有online_user_list_item.xml,它的內容如下:

Xhtml代碼

  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  2. android:layout_width="wrap_content"    
  3. android:layout_height="wrap_content"     
  4. android:id="@+id/online_user_list_item_textview" >  
  5. </TextView>  

則android.R.layout.simple_expandable_list_item_1換成R.layout.online_user_list_item。

如果我們想要使用更復雜一點的layout,而不僅是隻有一個textview,那就要用下面這種。

二、樣式豐富但內容簡單的。

layout下的online_user_list_item.xml內容如下:

Xhtml代碼

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content">  
  5. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"  android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView>  
  6. <Button  
  7.     android:text="button"  
  8.     android:layout_width="wrap_content"  
  9.     android:layout_height="wrap_content">  
  10. </Button>  
  11. </LinearLayout>  

裏面含有的textview是我們想要展示內容的地方。那麼構建ArrayAdapter時,應該這樣寫:

Java代碼

  1. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);  

如果我們需要展示的內容是一僅一個textview承載不了的,還需要其它組件,怎麼辦?我們可以自定義。

三、內容豐富的(自定義ArrayAdapter)。

這就需要寫一個類繼承自ArrayAdapter並且重寫getView方法。上代碼:

Java代碼

  1. public class UserListAdapter extends ArrayAdapter<User> {   
  2.     private int resourceId;   
  3.     public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {   
  4.         super(context, textViewResourceId, objects);   
  5.         this.resourceId = textViewResourceId;   
  6.     }   
  7.        
  8.     @Override  
  9.     public View getView(int position, View convertView, ViewGroup parent){   
  10.         User user = getItem(position);   
  11.         LinearLayout userListItem = new LinearLayout(getContext());   
  12.         String inflater = Context.LAYOUT_INFLATER_SERVICE;    
  13.         LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);    
  14.         vi.inflate(resourceId, userListItem, true);   
  15.         TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);   
  16.         TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);   
  17.         TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);   
  18.         tvUsername.setText(user.getUsername());   
  19.         tvAskedNum.setText(String.valueOf(user.getAskedNum()));   
  20.         tvLastMsg.setText(user.getLastMsg());   
  21.         return userListItem;   
  22.     }   
  23. }  

activity裏就這樣寫

Java代碼

  1. List<User> users = new ArrayList<User>();   
  2.         User user = new User();   
  3.         user.setAskedNum(8);   
  4.         user.setLastMsg("hello");   
  5.         user.setUsername("pxx");   
  6.         users.add(user);   
  7.         users.add(user);   
  8.         users.add(user);   
  9.         UserListAdapter adapter = new UserListAdapter(this,R.layout.online_user_list_item,users);   

        listview.setAdapter(adapter);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章