我有axml文件上的按钮,它从片段调用。我怎样才能使用按钮,例如我想要从ID为“cobaButton”的按钮获得onclick命令。我正在使用Visual Studio 2015和Xamarin开发Android应用程序。片段visual studio上的访问按钮xamarin
这是我的活动
public class MenuUtama : Activity
{
private DrawerLayout _drawer;
private MyActionBarDrawerToggle _drawerToggle;
private ListView _drawerList;
private string _drawerTitle;
private string _title;
private string[] _planetTitles;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.MenuUtama);
_title = _drawerTitle = Title;
_planetTitles = Resources.GetStringArray(Resource.Array.PlanetsArray);
_drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
_drawerList = FindViewById<ListView>(Resource.Id.left_drawer);
_drawer.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int)GravityFlags.Start);
_drawerList.Adapter = new ArrayAdapter<string>(this,
Resource.Layout.DrawerListItem, _planetTitles);
_drawerList.ItemClick += (sender, args) => SelectItem(args.Position);
ActionBar.SetDisplayHomeAsUpEnabled(true);
ActionBar.SetHomeButtonEnabled(true);
//DrawerToggle is the animation that happens with the indicator next to the
//ActionBar icon. You can choose not to use this.
_drawerToggle = new MyActionBarDrawerToggle(this, _drawer,
Resource.Drawable.ic_drawer,
Resource.String.DrawerOpen,
Resource.String.DrawerClose);
//You can alternatively use _drawer.DrawerClosed here
_drawerToggle.DrawerClosed += delegate
{
ActionBar.Title = _title;
InvalidateOptionsMenu();
};
//You can alternatively use _drawer.DrawerOpened here
_drawerToggle.DrawerOpened += delegate
{
ActionBar.Title = _drawerTitle;
InvalidateOptionsMenu();
};
_drawer.SetDrawerListener(_drawerToggle);
if (null == savedInstanceState)
SelectItem(0);
}
private void SelectItem(int position)
{
var fragment = new PlanetFragment();
var arguments = new Bundle();
arguments.PutInt(PlanetFragment.ArgPlanetNumber, position);
fragment.Arguments = arguments;
FragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
_drawerList.SetItemChecked(position, true);
ActionBar.Title = _title = _planetTitles[position];
_drawer.CloseDrawer(_drawerList);
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
base.OnPostCreate(savedInstanceState);
_drawerToggle.SyncState();
}
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
_drawerToggle.OnConfigurationChanged(newConfig);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.main, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnPrepareOptionsMenu(IMenu menu)
{
var drawerOpen = _drawer.IsDrawerOpen(Resource.Id.left_drawer);
menu.FindItem(Resource.Id.action_websearch).SetVisible(!drawerOpen);
return base.OnPrepareOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (_drawerToggle.OnOptionsItemSelected(item))
return true;
switch (item.ItemId)
{
case Resource.Id.action_websearch:
{
var intent = new Intent(Intent.ActionWebSearch);
intent.PutExtra(SearchManager.Query, ActionBar.Title);
if ((intent.ResolveActivity(PackageManager)) != null)
StartActivity(intent);
else
Toast.MakeText(this, Resource.String.app_not_available, ToastLength.Long).Show();
return true;
}
case Resource.Id.action_slidingpane:
{
var intent = new Intent(this, typeof(SlidingPaneLayoutActivity));
intent.AddFlags(ActivityFlags.ClearTop);
StartActivity(intent);
return true;
}
default:
return base.OnOptionsItemSelected(item);
}
}
}
这是我的片断文件
public class PlanetFragment : DialogFragment
{
public static string ArgPlanetNumber = "planet_number";
ConnectDB db = new ConnectDB();
public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
{
var i = Arguments.GetInt(ArgPlanetNumber);
if (i == 0)
{
var rootView = p0.Inflate(Resource.Layout.HomePermainan, p1, false);
return rootView;
}
else
{
var rootView = p0.Inflate(Resource.Layout.MyProfile, p1, false);
return rootView;
}
}
}
,这是我axml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/cobaButton" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button3" />
</LinearLayout>
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button4" />
它不片段的作品,我错过了什么? – neneo
得到misstype,它现在的作品,谢谢:D – neneo