我要补充一个ID? [英] Should I add an ID?

查看:115
本文介绍了我要补充一个ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前的code。

 包com.aaaa.ffff;进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.View;
进口android.view.ViewGroup;公共类HomeFragment扩展片段{公共HomeFragment(){}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);//为了使该方法接听电话
setHasOptionsMenu(真);
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,FALSE);返回rootView;
}@覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
inflater.inflate(R.menu.main,菜单);
}}

它显示一个操作栏图标。我从这里

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    在操作栏项目//处理presses
    开关(item.getItemId()){
        案例R.id.action_search:
            OpenSearch的();
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}

我怎么代替的OpenSearch ?难道是我的活动?另外,我需要添加其他切换活动?我是新来这的方式,所以任何帮助将是巨大的。


解决方案

  OpenSearch的();基本上是,当您单击菜单中的特定按钮调用的方法。

您可以替换 OpenSearch的()与您要拨打第二次活动,当你preSS在这个code菜单的按钮:

 意向意图=新意图(MainActivity.this,SecondActivity.class);
startActivity(意向);

基本上的OpenSearch 表示,搜索功能将被执行,当您单击操作栏中的搜索图标。

您可以Google一下。有很多例子。

This is my current code.

    package com.aaaa.ffff;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

public HomeFragment(){}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//In order for this method to receive calls
setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}

}

It displays a action bar icon. I got this code from here

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

What do I substitute for openSearch? Would it be my activity? Also do I need to add anything else to switch activities? I'm new to this by the way, so any help would be great.

解决方案

openSearch(); is basically a method called when you click that particular button in the menu.

You can substitute openSearch() with a second activity you want to call when you press that button in the menu by this code :

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

Basically openSearch denotes that a search function is to be performed when you click search icon in the action bar.

You can Google it. There are many examples.

这篇关于我要补充一个ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆