原文 Visual Studio跨平臺開發實戰(4) - Xamarin Android基本控制項介紹android
public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle) {
base.OnCreate (bundle);
InputStream input = Assets.Open ("my_asset.txt");}}web
SetContentView(Resource.Layout.TextView);api
SetContentView(Resource.Layout.EditText);框架
<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:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"/>
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/toggleButton1"
android:textOn="開"
android:textOff="關"
android:layout_marginBottom="6.7dp" />
<Switch
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOn="開"
android:textOff="關"
android:id="@+id/Switch1"
android:layout_marginRight="225.3dp" />ide
//載入頁面
SetContentView(Resource.Layout.SwitchToggle);工具
//宣告並取得控件實體
ToggleButton toggle = FindViewById<ToggleButton>(Resource.Id.toggleButton1);
Switch _switch = FindViewById<Switch>(Resource.Id.Switch1);
TextView msg = FindViewById<TextView>(Resource.Id.textView1);post
//處理Toggle Button的Click事件, 並將狀態顯示在TextView
toggle.Click+= (sender, e) => {
if (toggle.Checked) {
msg.Text = "目前Toggle Button的狀態是\"開\"";}
else{
msg.Text = "目前Toggle Button的狀態是\"關\"";};};spa
//處理Switch的Click事件, 並將狀態顯示在TextView
_switch.Click += (sender, e) => {
if (_switch.Checked) {
msg.Text = "目前Switch Button的狀態是\"開\"";}
else{
msg.Text = "目前Switch Button的狀態是\"關\"";};};設計
<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:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"/>
<Seekbar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Seekbar1"
android:layout_marginRight="48.0dp" />3d
//載入頁面
SetContentView(Resource.Layout.SeekBar);
//宣告並取得頁面上的控件
var msg = FindViewById<TextView>(Resource.Id.textView1);
var seekbar = FindViewById<SeekBar>(Resource.Id.seekBar1);
//將seekBar的最大值設定爲100
seekbar.Max = 100;
//處理SeekBar的ProgressChanged事件,並將目前的大小(進度)透過TextView呈現
seekbar.ProgressChanged += (sender,e) => {
msg.Text = string.Format("目前Seekbar的大小爲{0}",seekbar.Progress.ToString());
};