常用系統(tǒng)控件界面大合集
今天我用自己寫的一個(gè)Demo 和大家詳細(xì)介紹一個(gè)Android開(kāi)發(fā)中遇到的一些常用系統(tǒng)控件的使用技巧。
1.文本框TextView
TextView的作用是用來(lái)顯示一個(gè)文本框,下面我用兩種方式為大家呈現(xiàn)TextView, 第一種是通過(guò)xml布局文件呈現(xiàn) ,第二種是通過(guò)代碼來(lái)呈現(xiàn),由此可見(jiàn)Android 的界面開(kāi)發(fā)真的是非常靈活。
- public class TextViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.textview);
LinearLayout ll = (LinearLayout) findViewById(R.id.textviewll);
TextView textView = new TextView(this);
//設(shè)置顯示文字
textView.setText("從代碼中添加一個(gè)TextView");
//設(shè)置顯示顏色
textView.setTextColor(Color.WHITE);
//設(shè)置顯示字體大小
textView.setTextSize(18);
//設(shè)置顯示背景顏色
textView.setBackgroundColor(Color.BLUE);
//設(shè)置錨點(diǎn)位置
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
//把這個(gè)view加入到布局當(dāng)中
ll.addView(textView);
super.onCreate(savedInstanceState);
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textviewll"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="@string/textView"
android:gravity="center_vertical|center_horizontal"
/>
</LinearLayout>
復(fù)制代碼 2.網(wǎng)頁(yè)框WebView
WebView可以實(shí)現(xiàn) 類似web的網(wǎng)頁(yè) 的系統(tǒng)控件 最主要的是可以使用html代碼,如訪問(wèn)網(wǎng)頁(yè)等。
- public class WebViewActivity extends Activity {
WebView webView = null;
static final String MIME_TYPE = "text/html";
static final String ENCODING = "utf-8";
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview);
webView.loadDataWithBaseURL(null,"<a href=http://blog.csdn.net/xys289187120>歡迎訪問(wèn)雨松MOMO的博客</a>", MIME_TYPE, ENCODING, null);
super.onCreate(savedInstanceState);
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textviewll"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="網(wǎng)頁(yè)框WebView測(cè)試"
android:gravity="center_vertical|center_horizontal"
/>
<WebView android:id="@+id/webview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
復(fù)制代碼 3.Menu菜單
Menu菜單在android系統(tǒng)控件中真的很具有特色 點(diǎn)擊以后會(huì)懸浮出一個(gè)菜單在次點(diǎn)擊菜單則會(huì)消失,今天我只是簡(jiǎn)單的介紹一下系統(tǒng)的Menu菜單, 其實(shí)Menu菜單可以做出非常好看的效果,比如半透明 自定義按鈕圖片等等,后面我會(huì)詳細(xì)的介紹menu菜單。
- public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.menuview);
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, Menu.NONE, "菜單1").setIcon(R.drawable.icon);
menu.add(0, 1, Menu.NONE, "菜單2").setIcon(R.drawable.icon);
menu.add(0, 2, Menu.NONE, "菜單3").setIcon(R.drawable.icon);
menu.add(0, 3, Menu.NONE, "菜單4").setIcon(R.drawable.icon);
menu.add(0, 4, Menu.NONE, "菜單5").setIcon(R.drawable.icon);
menu.add(0, 5, Menu.NONE, "菜單6").setIcon(R.drawable.icon);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Dialog(item.getItemId());
return super.onOptionsItemSelected(item);
}
private void Dialog(int message) {
new AlertDialog.Builder(this).setMessage(
"您單擊第【" + message + "】項(xiàng)Menu菜單項(xiàng).").show();
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="Menu菜單測(cè)試"
android:gravity="center_vertical|center_horizontal"
/>
</LinearLayout>
復(fù)制代碼 4.按鈕Button
第一個(gè)是繪制系統(tǒng)字的button, 第二個(gè)是帶圖片的button 。
- public class ButtonActivity extends Activity {
Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.buttonview);
mContext = this;
//普通按鈕
Button button0 = (Button)findViewById(R.id.buttonview0);
//設(shè)置按鈕文字顏色
button0.setTextColor(Color.BLUE);
//設(shè)置按鈕文字大小
button0.setTextSize(30);
//設(shè)置按鈕監(jiān)聽(tīng) 點(diǎn)擊事件
button0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(ButtonActivity.this, "您點(diǎn)擊了‘這是一個(gè)按鈕’", Toast.LENGTH_LONG).show();
}
});
//帶圖片的按鈕
ImageButton button1 = (ImageButton)findViewById(R.id.buttonview1);
//設(shè)置按鈕監(jiān)聽(tīng) 點(diǎn)擊事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(ButtonActivity.this, "您點(diǎn)擊了一個(gè)帶圖片的按鈕", Toast.LENGTH_LONG).show();
}
});
super.onCreate(savedInstanceState);
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="Button按鈕測(cè)試"
android:gravity="center_vertical|center_horizontal"
/>
<Button
android:id="@+id/buttonview0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="這是一個(gè)按鈕"
/>
<ImageButton
android:id="@+id/buttonview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>
復(fù)制代碼 5.編輯框EditView
編輯框在實(shí)際開(kāi)發(fā)中用到的非常普遍 比如登錄 輸入賬號(hào) 密碼 等等。
- public class EditTextActivity extends Activity {
Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.editview);
mContext = this;
//帳號(hào)
final EditText editText0 = (EditText)findViewById(R.id.editview0);
//密碼
final EditText editText1 = (EditText)findViewById(R.id.editview1);
//確認(rèn)按鈕
Button button = (Button)findViewById(R.id.editbutton0);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String username = editText0.getText().toString();
String password = editText1.getText().toString();
Toast.makeText(EditTextActivity.this, "用戶名:"+username +"密碼:"+ password, Toast.LENGTH_LONG).show();
}
});
super.onCreate(savedInstanceState);
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="EditText編輯框測(cè)試"
android:gravity="center_vertical|center_horizontal"
/>
<EditText
android:id="@+id/editview0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入帳號(hào)"
android:phoneNumber="true"
/>
<EditText
android:id="@+id/editview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入密碼"
android:password="true"
/>
<Button
android:id="@+id/editbutton0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確定"
/>
</LinearLayout>
復(fù)制代碼 6.單項(xiàng)選擇
使用RadioGroup 包住若干個(gè)RadioButton 來(lái)實(shí)現(xiàn)單項(xiàng)選擇。監(jiān)聽(tīng)每一個(gè)RadioGroup 就可以知道那個(gè)單選組中的第一個(gè)ID被按下。
- public class RadioActivity extends Activity {
Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.radioview);
mContext = this;
//單選組(只有在一個(gè)組中的按鈕可以單選)
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0);
//單選按鈕(第一組)
final RadioButton radioButton0 = (RadioButton)findViewById(R.id.radionButton0);
final RadioButton radioButton1 = (RadioButton)findViewById(R.id.radionButton1);
final RadioButton radioButton2 = (RadioButton)findViewById(R.id.radionButton2);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int checkID) {
if(radioButton0.getId() == checkID) {
Toast.makeText(RadioActivity.this, "您選中了第一組" + radioButton0.getText(), Toast.LENGTH_LONG).show();
}else if(radioButton1.getId() == checkID) {
Toast.makeText(RadioActivity.this, "您選中了第一組" + radioButton1.getText(), Toast.LENGTH_LONG).show();
}else if(radioButton2.getId() == checkID) {
Toast.makeText(RadioActivity.this, "您選中了第一組" + radioButton2.getText(), Toast.LENGTH_LONG).show();
}
}
});
RadioGroup radioGroup0 = (RadioGroup)findViewById(R.id.radion1);
//單選按鈕(第二組)
final RadioButton radioButton3 = (RadioButton)findViewById(R.id.radionButton3);
final RadioButton radioButton4 = (RadioButton)findViewById(R.id.radionButton4);
final RadioButton radioButton5 = (RadioButton)findViewById(R.id.radionButton5);
radioGroup0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int checkID) {
if(radioButton3.getId() == checkID) {
Toast.makeText(RadioActivity.this, "您選中了第二組" + radioButton3.getText(), Toast.LENGTH_LONG).show();
}else if(radioButton4.getId() == checkID) {
Toast.makeText(RadioActivity.this, "您選中了第二組" + radioButton4.getText(), Toast.LENGTH_LONG).show();
}else if(radioButton5.getId() == checkID) {
Toast.makeText(RadioActivity.this, "您選中了第二組" + radioButton5.getText(), Toast.LENGTH_LONG).show();
}
}
});
super.onCreate(savedInstanceState);
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="單項(xiàng)選擇測(cè)試第一組"
android:gravity="center_vertical|center_horizontal"
/>
<RadioGroup
android:id="@+id/radion0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radionButton0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item0"
/>
<RadioButton
android:id="@+id/radionButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item1"
/>
<RadioButton
android:id="@+id/radionButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item2"
/>
</RadioGroup>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="單項(xiàng)選擇測(cè)試第二組"
android:gravity="center_vertical|center_horizontal"
/>
<RadioGroup
android:id="@+id/radion1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radionButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item3"
/>
<RadioButton
android:id="@+id/radionButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item4"
/>
<RadioButton
android:id="@+id/radionButton5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item5"
/>
</RadioGroup>
</LinearLayout>
復(fù)制代碼 7.多項(xiàng)選擇
使用系統(tǒng)控件Checkbox 監(jiān)聽(tīng)每一個(gè)checkbox 的點(diǎn)擊事件就可以確定那幾個(gè)選項(xiàng)被選擇了。
- public class CheckboxActivity extends Activity {
//用來(lái)儲(chǔ)存選中的內(nèi)容
ArrayList <String>item = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.checkboxview);
CheckBox checkbox0 = (CheckBox)findViewById(R.id.checkboxview0);
CheckBox checkbox1 = (CheckBox)findViewById(R.id.checkboxview1);
CheckBox checkbox2 = (CheckBox)findViewById(R.id.checkboxview2);
CheckBox checkbox3 = (CheckBox)findViewById(R.id.checkboxview3);
Button button = (Button)findViewById(R.id.checkboxbutton);
//對(duì)checkbox進(jìn)行監(jiān)聽(tīng)
checkbox0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean arg1) {
String str = button.getText().toString();
if (button.isChecked()) {
item.add(str);
} else {
item.remove(str);
}
}
});
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean arg1) {
String str = button.getText().toString();
if (button.isChecked()) {
item.add(str);
} else {
item.remove(str);
}
}
});
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean arg1) {
String str = button.getText().toString();
if (button.isChecked()) {
item.add(str);
} else {
item.remove(str);
}
}
});
checkbox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean arg1) {
String str = button.getText().toString();
if (button.isChecked()) {
item.add(str);
} else {
item.remove(str);
}
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String str = item.toString();
Toast.makeText(CheckboxActivity.this, "您選中了" + str, Toast.LENGTH_LONG).show();
}
});
super.onCreate(savedInstanceState);
}
}
復(fù)制代碼 - <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="多項(xiàng)選擇測(cè)試"
android:gravity="center_vertical|center_horizontal"
/>
<CheckBox
android:id="@+id/checkboxview0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item0"
/>
<CheckBox
android:id="@+id/checkboxview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item1"
/>
<CheckBox
android:id="@+id/checkboxview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item2"
/>
<CheckBox
android:id="@+id/checkboxview3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="item3"
/>
<Button
android:id="@+id/checkboxbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確定"
/>
</LinearLayout>
復(fù)制代碼 最后如果你還是覺(jué)得我寫的不夠詳細(xì) 看的不夠爽 不要緊我把源代碼的下載地址貼出來(lái) 歡迎大家一起討論學(xué)習(xí)
android常用系統(tǒng)控件.rar(156.28 KB, 下載次數(shù): 1495)[/I]2011-9-2 19:28 上傳點(diǎn)擊文件名 下載積分: 下載豆 -2 |