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);

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