我有一个带有后退按钮的可重复使用的布局。我想从BaseActivity处理我的后退按钮的点击事件。我将按钮的onClick设置为“headerClickHandler”,我在BaseActivity中有一个具有此名称的方法,但是当我单击该按钮时,错误表明没有headerClickHandler。怎么了? 这是我的Header.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/txtHeading"
android:text="@string/general_title"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_height="30dp"
android:layout_width="200dp"/>
<Button android:id="@+id/btnBack"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="30dp"
android:layout_width="100dp"
android:background="@color/red_hover"
android:text="@string/return_value"
android:onClick="headerClickHandler"
/>
</RelativeLayout>
这是我的BaseActivity:
public class BaseActivity extends Activity {
public void headerClickHandler(View v) {
switch (v.getId()) {
case R.id.btnBack:
Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
这是我的活动:
public class PersonnelInfo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personnel_info);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.personnel_info, menu);
return true;
}
}
答案 0 :(得分:1)
从xml
中删除android:onClick="headerClickHandler"
在基础活动中:
public class BaseActivity extends Activity {
public OnClickListener headerClickHandler = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnBack:
Toast.makeText(BaseActivity.this, "TEST", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
};
}
在派生活动中:
Button btnBack = (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(headerClickHandler);
答案 1 :(得分:0)
我认为问题在于您忘记了从BaseActivity扩展
public class MainActivity extends BaseActivity
我试过,一切都很好
MainActivity
package com.stackoverflow.reusableview;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
BaseActivity
package com.stackoverflow.reusableview;
import android.app.Activity;
import android.view.View;
import android.widget.Toast;
public class BaseActivity extends Activity {
public void headerClickHandler(View v) {
switch (v.getId()) {
case R.id.btnBack:
Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/txtHeading"
android:text="@string/general_title"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_height="30dp"
android:layout_width="200dp"/>
<Button android:id="@+id/btnBack"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="30dp"
android:layout_width="100dp"
android:background="@android:color/black"
android:text="@string/return_value"
android:onClick="headerClickHandler"
/>
</RelativeLayout>
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<include layout="@layout/header"/>