这是代码: 这是一个几乎每个页面都包含的菜单,我想用这个菜单导航抛出页面,但onClicks不会开火我试着onTouch操作下来,一切都没有用。请帮助谢谢。
public class BottomNavBar extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_nav_bar);
ImageButton schedule = (ImageButton) findViewById(R.id.btmSchedule);
ImageButton map = (ImageButton) findViewById(R.id.btmMap);
final ImageButton liveFeed = (ImageButton) findViewById(R.id.btmLivefeed);
ImageButton message = (ImageButton) findViewById(R.id.btmMessage);
ImageButton profile = (ImageButton) findViewById(R.id.btmProfile);
schedule.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent intent = new Intent(getApplicationContext(), ScheduleActivity.class);
startActivity(intent);
}
if ((event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)) {
}
return false;
}
});
liveFeed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BottomNavBar.this, "startedLiveFeed", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),LiveFeedActivity.class);
startActivity(intent);
}
});
profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),UserProfile.class);
startActivity(intent);
}
});
schedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ScheduleActivity.class);
startActivity(intent);
}
});
message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),NewsActivity.class);
startActivity(intent);
}
});
map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),VideosActivity.class);
startActivity(intent);
}
});
}
xml parent
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:focusable="false">
<include layout="@layout/content_nav_bar" />
父内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="5"
android:id="@+id/content_navbar"
android:clickable="true"
android:focusable="false">
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btmSchedule"
android:src="@drawable/schedule"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="@string/asd" />
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/map"
android:id="@+id/btmMap"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="@string/asd" />
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/livefeed"
android:id="@+id/btmLivefeed"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="@string/asd"
android:padding="5dp"/>
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/message"
android:id="@+id/btmMessage"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="@string/asd" />
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/profile"
android:id="@+id/btmProfile"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="@string/asd" />
答案 0 :(得分:1)
public class FasTestFragment extends UtuBaseFragment implements
View.OnClickListener {
@Bind(R.id.btmSchedule)
ImageButton btmSchedule;
@Bind(R.id.btmMap)
ImageButton btmMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_include, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btmSchedule.setOnClickListener(this);
btmMap.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btmSchedule) {
System.out.println("****Schedule clicked.");
}else if (id == R.id.btmMap) {
System.out.println("****map clicked.");
}
}
}
//的test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="5"
android:id="@+id/content_navbar"
android:clickable="true"
android:focusable="false">
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btmSchedule"
android:src="@drawable/fas_void"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="123" />
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/fas_user"
android:id="@+id/btmMap"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="123" />
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/fas_enquiry"
android:id="@+id/btmLivefeed"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="123"
android:padding="5dp"/>
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/fas_payment"
android:id="@+id/btmMessage"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="123" />
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/fas_summary"
android:id="@+id/btmProfile"
android:background="#F0F4F7"
android:layout_gravity="center|left"
android:scaleType="fitXY"
android:clickable="true"
android:contentDescription="123" />
</LinearLayout>
// test_include.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:focusable="false">
<include layout="@layout/test" />
</LinearLayout>
//结果
07-27 11:51:51.641 492-492/com.UTU I/System.out: ****Schedule clicked.
07-27 11:51:51.761 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:51.831 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:51.831 492-492/com.UTU I/System.out: ****Schedule clicked.
07-27 11:51:53.271 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:53.351 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:53.351 492-492/com.UTU I/System.out: ****map clicked.
07-27 11:51:53.461 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:53.531 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:53.531 492-492/com.UTU I/System.out: ****map clicked.
07-27 11:51:53.641 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:53.711 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:53.721 492-492/com.UTU I/System.out: ****map clicked.
07-27 11:51:53.841 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:53.901 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:53.901 492-492/com.UTU I/System.out: ****map clicked.
07-27 11:51:54.031 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:54.101 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:54.101 492-492/com.UTU I/System.out: ****map clicked.
07-27 11:51:54.211 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 0
07-27 11:51:54.271 492-492/com.UTU D/ViewRootImpl: ViewPostImeInputStage processPointer 1
07-27 11:51:54.271 492-492/com.UTU I/System.out: ****map clicked.