问题描述
我正在开发一个 xamarin android 应用程序,我曾经在所有活动中调用 Header 活动.我的代码是 Fallows
I am developing a xamarin android application, where I used to call a Header activity in all Activities. My code is as Fallows
我的 Main.axml
My Main.axml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Header aligned to top -->
<include layout="@layout/Header"
android:id="@+id/includeheader"
android:layout_alignParentTop="true" />
<!-- Content below header and above footer -->
<include layout="@layout/Content"
android:id="@+id/includecontent" />
<!-- Footer aligned to bottom -->
<include layout="@layout/Footer"
android:id="@+id/includefooter"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
我的 Header.axml
My Header.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="50dip">
<TableRow
android:background="#2c2c2c"
android:padding="10dip">
<TextView
android:id="@+id/scanHome"
android:layout_width="0dip"
android:layout_weight="2.5"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:text="" />
<Button
android:id="@+id/Settings"
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_marginLeft="10dip"
android:layout_weight="0.17"
android:gravity="center"
android:width="35dip"
android:clickable="true"
android:onClick="SettingsClick"/>
<Button
android:id="@+id/logout"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="0.27"
android:gravity="center"
android:width="40dip" />
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.class
MainActivity.class
namespace LayoutApp
{
[Activity(Label = "LayoutApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Header
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
setHeading("Scan Home");
}
}
}
Header.class
Header.class
[Activity(Label = "LayoutApp", MainLauncher = false)]
public abstract class Header : Activity , View.IOnClickListener
{
private TextView HeaderText;
private Button Settings;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Header);
Settings = FindViewById<Button>(Resource.Id.Settings);
Settings.Click += delegate
{
};
}
protected void setHeading(string text)
{
if (HeaderText == null)
HeaderText = FindViewById<TextView>(Resource.Id.scanHome);
if (HeaderText != null)
HeaderText.Text = text;
}
public void SettingsClick()
{
}
}
因此,我在 MainActivity 中使用 Header Activity,就像在原生 android 中使用 include 属性一样.当我加载 Main Launcher 时,Header 也会显示,但 MainActivity 中的单击事件不起作用,因为文本是从 setHeading 方法应用的.
Hence I am using Header Activity in MainActivity like in native android using include Property. When I load Main Launcher, Header is also displaying but click events are not working from the MainActivity where as text is applying from setHeading method.
调试时,错误填充为java.lang.illegalistateexception:在视图类上的 onclick 处理程序的活动类中找不到方法 SettingsClick(View)".
When debugging , error is populating as 'java.lang.illegalistateexception: could not find a method SettingsClick(View) in the activityclass for onclick handler on view class'.
所以,我的问题是我想获得 Header 的点击事件.
So, my issue here is I would like to get click events of Header.
推荐答案
@kumar Sudheer,我不知道 xamarin 的开发,但是通过查看代码和得到的异常我可以理解你想要做什么.
@kumar Sudheer, I don't know the xamarin development, but I can understand what you want to do by looking at the code and exception you get.
只需在标题活动的 SettingClick
方法中将 View
对象作为参数传递
Just pass the View
object as parameter in the SettingClick
method of your header activity
public void SettingsClick(View v)
{
}
在 android 中,当您在布局文件中定义点击处理程序时,该方法的签名将是 public void (View v) {}
.
In android when you define the click handler in layout file then the signature of that method would be public void <clickHandler>(View v) {}
.
您没有在方法中传递 View
参数,这就是系统无法在 Activity 中找到您的方法的原因,这就是您收到 java.lang.IllegaliStateException
You've not passed the View
parameter in the method that's why system is unable to find your method in Activity and that's why you are getting the java.lang.IllegaliStateException
这篇关于带有 Xamarin android 中所有活动的标题的单击事件的通用标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!