将onclick监听器添加到工具栏上的图标

时间:2018-01-09 12:58:01

标签: android onclicklistener android-toolbar

[我的工具栏] [1]

有人有任何解决方案吗?

3 个答案:

答案 0 :(得分:2)

我见过你的工具栏。 这是我的解决方案:

  • 首先创建菜单xml资源文件。(这将包含您的“心形图标”,res / menu / menu_example.xml)

根据需要命名,例如,我将其命名为menu_example。

<强> menu_example.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
       android:id="@+id/action_addfav"
       android:title=""
       app:showAsAction="always"
       android:icon="@drawable/YOUR_ADDFAV_DRAWABLE" />
</menu>
  • 然后创建您的活动代码

<强> activity_example.java

package com.example;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class activity_example extends AppCompatActivity {


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

        //Layout xml init
        setContentView(R.layout.activity_example);

        //Actionbar
        //Back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }



    //Inflate the menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_example, menu);
        return true;
    }

    //Handling Action Bar button click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            //Back button
            case android.R.id.home:
                //If this activity started from other activity
                finish();

            /*If you wish to open new activity and close this one
            startNewActivity();
            */
                return true;
            case R.id.action_addfav:
                //addfav (heart icon) was clicked, Insert your after click code here.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void startNewActivity(){
        Intent intent = new Intent(this,ACTIVITY_YOU_WANT_TO_START.class);
        startActivity(intent);
        finish();
    }

}
  • 活动布局xml

(您可以将根布局更改为您想要的任何布局)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- YOUR LAYOUT HERE -->
    </RelativeLayout>
  • 最后,请确保您的应用在主题中有工具栏 转到res / values / styles.xml

<强> styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <! -- Parent got to have actionbar in order for toolbat to appear -->

</style>

如果您希望工具栏仅在一个特定活动中,请查看:Apply a theme to an activity in Android?

答案 1 :(得分:0)

您需要检查 R.id.img_bar_back 。天气它是您正在寻找的ImageView ID或其他东西。这可能是由于查找错误的视图ID而发生的。

答案 2 :(得分:0)

SELECT MIN(cr.cr_id) AS cr_id, cr.acc_id
FROM cr
WHERE acc_id IN (100, 200)
AND cr.start_time IN
(SELECT MAX(sub.start_time)
 FROM cr sub
 WHERE cr.acc_id = sub.acc_id)
GROUP BY cr.acc_id