ActionBar单击Switchbutton Android

时间:2015-03-12 14:48:19

标签: android android-actionbar

单击操作栏上的开关按钮有问题。这是我的代码

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=""/>

和switchbutton的布局:

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

当我点击开关按钮时,这个吐司没有显示。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

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

将您的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;
            }
        });
    }
...
}

答案 1 :(得分:0)

为什么要这样?

当您从选项菜单中选择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);
}

}