菜单项的Onclick提供有线吐司

时间:2014-04-24 19:51:46

标签: android android-fragments navigation-drawer fragment-tab-host

我正在使用Drawer功能,我正在使用片段的Fragment-TabHost功能 我正在使用Sherlock动作库


代码运行正常,但功能中有一个错误::当我点击actionBar的菜单项时,会显示如下图所示的吐司类型对话框,我从未在代码中使用过吐司

节点::按住菜单项3秒......然后弹出此对话框

enter image description here

MainActivity.java

public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{

    private DrawerLayout drawlayout=null;
    private ListView listview=null;
    private ActionBarDrawerToggle actbardrawertoggle=null;

    private String[] myfriendname=null;
    private String[] myfriendnickname=null;
    private int[] photo=null;
    //Fragment fragment1 = new Fragment1();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         myfriendname = new String[] { "Sunil Gupta", "Abhishek Tripathi","Sandeep Pal", "Amit Verma" };
         myfriendnickname = new String[] { "sunil android", "Abhi cool","Sandy duffer", "Budhiya jokar"};
         photo = new int[] { R.drawable.sunil, R.drawable.abhi, R.drawable.sandy, R.drawable.amit};

         drawlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
         listview = (ListView) findViewById(R.id.listview_drawer);

         getSupportActionBar().setHomeButtonEnabled(true);
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         getSupportActionBar().setDisplayShowHomeEnabled(true);
         getSupportActionBar().setDisplayShowTitleEnabled(true);

         drawlayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
         drawlayout.setBackgroundColor(Color.WHITE);

         MenuListAdapter menuadapter=new MenuListAdapter(getApplicationContext(), myfriendname, myfriendnickname, photo); 
         listview.setAdapter(menuadapter);

         actbardrawertoggle= new ActionBarDrawerToggle(this, drawlayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)
            {
             public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }

         };
         drawlayout.setDrawerListener(actbardrawertoggle);

         listview.setOnItemClickListener(this);

         if (savedInstanceState == null) {
             selectItem(0);
         }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actbardrawertoggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actbardrawertoggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        selectItem(position);

    }


     private void selectItem(int position) {

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

            // Locate Position
            switch (position) {
            case 0:
                Fragment1 fragment1=new Fragment1();
                ft.replace(R.id.content_frame, fragment1);
                Bundle b = new Bundle();
                b.putString("name",myfriendname[position]);
                b.putInt("photo",photo[position]);
                fragment1.setArguments(b);
                break;
            case 1:
                Fragment2 fragment2=new Fragment2();
                ft.replace(R.id.content_frame, fragment2);
                Bundle b1 = new Bundle();
                b1.putString("name",myfriendname[position]);
                b1.putInt("photo",photo[position]);
                fragment2.setArguments(b1);
                break;
            case 2:
                Fragment3 fragment3=new Fragment3();
                ft.replace(R.id.content_frame, fragment3);
                Bundle b2 = new Bundle();
                b2.putString("name",myfriendname[position]);
                b2.putInt("photo",photo[position]);
                fragment3.setArguments(b2);
                break;
            case 3:
                Fragment4 fragment4=new Fragment4();
                ft.replace(R.id.content_frame, fragment4);
                Bundle b3 = new Bundle();
                b3.putString("name",myfriendname[position]);
                b3.putInt("photo",photo[position]);
                fragment4.setArguments(b3);
                break;
            }
            ft.commit();
            listview.setItemChecked(position, true);
            setTitle(myfriendname[position]);
            drawlayout.closeDrawer(listview);
        }

     @Override
    public boolean onOptionsItemSelected(MenuItem item) {

         if(item.getItemId()==android.R.id.home)
         {
             if(drawlayout.isDrawerOpen(listview))
             {
                 drawlayout.closeDrawer(listview);
             }
             else {
                drawlayout.openDrawer(listview);
            }
         }
        return super.onOptionsItemSelected(item);
    }    


     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater menuInflater = getSupportMenuInflater();
            menuInflater.inflate(R.menu.actionbar_sort_menu, menu);

            return super.onCreateOptionsMenu(menu);
        }
}

2 个答案:

答案 0 :(得分:1)

当您长按MenuItem中的ActionBar时,Toast is displayed that contains the title of the MenuItemToast为空,因为您没有MenuItem的标题。

From the docs:

  

如果操作项仅显示图标,则用户可以长按   该项目显示一个显示动作标题的工具提示。

答案 1 :(得分:0)

这不是错误。它的设计与此类似,它与您在此处发布的所有文件无关,但与您的菜单文件名为actionbar_sort_menu无关。 在这个文件中你可能有这样的东西:

<item
    android:id="@+id/menu_delete"
    android:orderInCategory="100"
    android:showAsAction="ifRoom|withText"
    android:title="@string/menu_delete"
    android:icon="@drawable/ic_white_trash"/>

所有你需要做的就是不使用标题。但是如果你想看到该项目的描述,那么为它写一些东西。