slidingmenu onclickitems(android)

mcdcgff0  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(219)

我使用这个库,它工作得很好,但我不知道如何创建如果我在菜单输出文本中单击项目1,如果我在菜单输出文本2中单击项目2。。。
这是我的密码:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("Click", "Click");
        switch (item.getItemId()) {
            case android.R.id.home:
                menu.toggle(true);
                return true; 

            case R.id.textView1:
                Log.e("textView1", "textView1");
                break; 

            case R.id.textView2:
                Log.e("textView2", "textView2");
                break;

            case R.id.textView3:
                Log.e("textView3", "textView3");
                break;

        default:
            break;
        }

        return super.onOptionsItemSelected(item);
    }

升级版
菜单\u flame.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="500dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@color/back">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:src="@drawable/abc_ratingbar_full_material" />

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/menu_1"
        android:layout_gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abc_ratingbar_full_material" />

         <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/menu_2"
        android:layout_gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abc_ratingbar_full_material" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/menu_3"
        android:layout_gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abc_ratingbar_full_material" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/menu_4"
        android:layout_gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

</LinearLayout>

活动主.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="com.example.testdesigne.MainActivity" >

      <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />

</RelativeLayout>

主要内容:

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            menu = new SlidingMenu(this);
            menu.setMode(SlidingMenu.LEFT);
            menu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
            menu.setShadowDrawable(R.drawable.slidemenu_shadowgradient);
            menu.setShadowWidth(15);
            menu.setFadeDegree(0.0f);
            menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
            menu.setBehindWidth(300);
            menu.setMenu(R.drawable.menu_frame);

            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("Click", "Click");

        switch (item.getItemId()) {

            case android.R.id.home:
                Log.e("item",  Integer.toString(item.getItemId()));
                menu.toggle(true);
                return true; 

            case R.id.textView1:
                Log.e("textView1", "textView1");
                break; 

            case R.id.textView2:
                Log.e("textView2", "textView2");
                break;

            case R.id.textView3:
                Log.e("textView3", "textView3");
                break;
        }

        return super.onOptionsItemSelected(item);
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题