以下内容全部或部分转自或参考于网络。
有了学习TextView控件的基础,打算接下来多学习几个控件。
一、Button控件
新建Android应用时,会自带一个Button控件,对应代码如下
protected override void OnCreate( Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView( Resource.Layout .Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button >(Resource. Id.MyButton);
button.Click += delegate { button.Text = string .Format("{0} clicks!", count++); };
}
二、EditText控件
在工具箱内分类归到了“Text Fields”分组中:Number,Password,Phone等。
三、Spinner控件
下面只列出了代码
public class MainActivity : Activity
{
Spinner state;
TextView tvsp;
ArrayAdapter<string > aas;
protected override void OnCreate( Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView( Resource.Layout .Main);
// Get our button from the layout resource,
// and attach an event to it
state = FindViewById< Spinner>(Resource .Id.sp);
tvsp = FindViewById< TextView>(Resource .Id.tvsp);
aas = new ArrayAdapter <string>( this,
Android. Resource.Layout .SimpleDropDownItem1Line);
state.Adapter = aas;
aas.Add( String.Empty);
aas.Add( "Alabbama");
aas.Add( "California");
aas.Add( "texas");
state.ItemSelected += new EventHandler <AdapterView. ItemSelectedEventArgs>(sp_ItemSelected);
}
void sp_ItemSelected(object sender, AdapterView. ItemSelectedEventArgs e)
{
tvsp.Text = Convert.ToString(aas.GetItem(e.Position));
}
}
代码中AdapterView. ItemSelectedEventArgs和书上不一样(没有AdapterView 部分),估计是版本升级的原因。