ActionBar 单击 Switchbutton Android

ActionBar click Switchbutton Android

单击操作栏上的切换按钮时出现问题。这是我的代码

public class MainActivity extends ActionBarActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item = menu.findItem(R.id.abc);
        item.setActionView(R.layout.testing);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.abc:
                Toast.makeText(getApplicationContext(), "ABOUT.", Toast.LENGTH_SHORT).show();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的菜单:menu_main

<item
    android:id="@+id/abc"
    app:showAsAction="ifRoom"
    app:actionLayout="@layout/test"
    android:title=""/>

开关按钮的布局:

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switchForActionBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />

当我点击切换按钮时,这个 toast 没有显示。有帮助吗?

那是因为您还没有为 SwitchCompat 视图定义 OnTouchListener

将您的 onCreate 方法代码更改为

public class MainActivity extends ActionBarActivity {

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

        SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switchForActionBar);
        switchCompat.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Toast.makeText(this, "ABOUT.", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
...
}

为什么要这样做???

当您从选项菜单中 select 带有 id:abc 的项目时,会显示 Toast 消息。

在您的代码中,没有证据表明在您单击切换按钮时会触发某个操作。

对于每个按钮 - 切换按钮 - 图像按钮等...您应该创建按钮对象并设置在单击按钮时要执行的操作。

onOptionsItemSelected 方法仅处理标准选项菜单。

public class MainActivity extends ActionBarActivity {

//declare the global variable switchcompat
 SwitchCompat switchCompat;

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

    //contruct the object from yhe one declared in the xml
    switchCompat = (SwitchCompat) findViewById(R.id.switchForActionBar);

    //set the onClickListener that define what to do when the view is                  clicked
    switchCompat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(this, "Some message here",                                     Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem item = menu.findItem(R.id.abc);
    item.setActionView(R.layout.testing);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.abc:
            Toast.makeText(getApplicationContext(), "ABOUT.", Toast.LENGTH_SHORT).show();
        break;
    }
    return super.onOptionsItemSelected(item);
}

}