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裏。
一、簡單的。
這樣的列表的每一行都只有一行文字。
- // 當然listview 也可以是在layout裏寫好,然後findViewById()獲取出來,這樣的話後面就不需setContentView(listview);
- ListView listview = new ListView(this);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);
- adapter.add("string1");
- adapter.add("haha");
- adapter.add("heihei");
- listview.setAdapter(adapter);
- 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,它的內容如下:
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/online_user_list_item_textview" >
- </TextView>
則android.R.layout.simple_expandable_list_item_1換成R.layout.online_user_list_item。
如果我們想要使用更復雜一點的layout,而不僅是隻有一個textview,那就要用下面這種。
二、樣式豐富但內容簡單的。
layout下的online_user_list_item.xml內容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView>
- <Button
- android:text="button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </Button>
- </LinearLayout>
裏面含有的textview是我們想要展示內容的地方。那麼構建ArrayAdapter時,應該這樣寫:
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);
如果我們需要展示的內容是一僅一個textview承載不了的,還需要其它組件,怎麼辦?我們可以自定義。
三、內容豐富的(自定義ArrayAdapter)。
這就需要寫一個類繼承自ArrayAdapter並且重寫getView方法。上代碼:
- public class UserListAdapter extends ArrayAdapter<User> {
- private int resourceId;
- public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {
- super(context, textViewResourceId, objects);
- this.resourceId = textViewResourceId;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent){
- User user = getItem(position);
- LinearLayout userListItem = new LinearLayout(getContext());
- String inflater = Context.LAYOUT_INFLATER_SERVICE;
- LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
- vi.inflate(resourceId, userListItem, true);
- TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);
- TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);
- TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);
- tvUsername.setText(user.getUsername());
- tvAskedNum.setText(String.valueOf(user.getAskedNum()));
- tvLastMsg.setText(user.getLastMsg());
- return userListItem;
- }
- }
activity裏就這樣寫
- List<User> users = new ArrayList<User>();
- User user = new User();
- user.setAskedNum(8);
- user.setLastMsg("hello");
- user.setUsername("pxx");
- users.add(user);
- users.add(user);
- users.add(user);
- UserListAdapter adapter = new UserListAdapter(this,R.layout.online_user_list_item,users);
listview.setAdapter(adapter);