Android 中如何调度点击事件

发布于 2024-11-26 07:01:06 字数 1237 浏览 3 评论 0原文

我有一个包含按钮的自定义导航栏,我将调度点击事件,以便包含我的导航栏的活动可以响应点击

    public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
        Context mycontext;
        View convertview;
        ImageButton searchadresse;

        public BarrePersonnalisee(Context context, AttributeSet attrs) {
            super(context, attrs);
            mycontext=context;
            convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
            searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
            searchadresse.setOnClickListener(this);
        }

        public void onClick(View arg0) {
            switch (arg0.getId()) {
            case R.id.searchadresse:
                //I want to dispatch this event
                    break;
                   }
            }
    ....
    }

public class TaxiMapActivity extends MapActivity{

    BarrePersonnalisee barre;

    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);
        //task to do here


}

有人可以帮忙吗?

I have a custom navigation bar that contains a button, I would dispatch the click event so that the activity that contains my navigation bar can respond to click

    public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
        Context mycontext;
        View convertview;
        ImageButton searchadresse;

        public BarrePersonnalisee(Context context, AttributeSet attrs) {
            super(context, attrs);
            mycontext=context;
            convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
            searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
            searchadresse.setOnClickListener(this);
        }

        public void onClick(View arg0) {
            switch (arg0.getId()) {
            case R.id.searchadresse:
                //I want to dispatch this event
                    break;
                   }
            }
    ....
    }

public class TaxiMapActivity extends MapActivity{

    BarrePersonnalisee barre;

    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);
        //task to do here


}

can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅唱々樱花落 2024-12-03 07:01:07

使用此代码

            Button b=new Button(this);
            b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //what you want to do
            }
        })

Use this code

            Button b=new Button(this);
            b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //what you want to do
            }
        })
尛丟丟 2024-12-03 07:01:07

假设您需要将点击事件附加到导航栏中的按钮,您可以使用 setOnClickListener() 方法,并实现 onClick() 方法,例如

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //define what you want to do here
            Toast.makeText(getApplicationContext(), "Works", Toast.LENGTH_SHORT).show();
        }
    });

:当您单击按钮时,将显示一个提示“有效”的提示。

Assuming that you need to attach click event to a button inside your navigation bar, you can use setOnClickListener() method, and implements the onClick() method, for example:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //define what you want to do here
            Toast.makeText(getApplicationContext(), "Works", Toast.LENGTH_SHORT).show();
        }
    });

that will show a toast saying "Works" when you click the button.

池木 2024-12-03 07:01:06

我相信您正在寻找的是 button.performClick()

如果您不想要点击声音,您可以这样做:

button.setSoundEffectsEnabled(false)

I believe what you are looking for is button.performClick()

and if you don't want the click sound, you can do:

button.setSoundEffectsEnabled(false)
待天淡蓝洁白时 2024-12-03 07:01:06

您实际上可以在这里使用EventBus: http://greenrobot.org/eventbus/

  1. 加载EventBus < /p>

    依赖项{
    编译 fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    编译'com.android.support:appcompat-v7:24.1.1'
    编译'com.android.support:support-v4:24.1.1'
    编译 'org.greenrobot:eventbus:3.0.0'
    }

  2. 创建新的事件类型类。我们将其命名为 BarrePersonnaliseeEvent。

公共类 BarrePersonnaliseeEvent {

私有布尔值_touchDown;
公共 BarrePersonnaliseeEvent(布尔 touchDown) {
    _touchDown = touchDown;
}

公共布尔 isTouchDown(){
    返回_touchDown;
} }
  1. 现在将事件传递给 EventBus

public class BarrePersonnalisee extends LinearLayout Implements OnClickListener {
上下文 mycontext;
查看转换视图;
图像按钮搜索地址;

    public BarrePersonnalisee(Context context, AttributeSet attrs) {
        super(context, attrs);
        mycontext=context;
        convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
        searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
        searchadresse.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.searchadresse:
            //I want to dispatch this event
                EventBus.getDefault().post(new BarrePersonnaliseeEvent(true));
                break;
               }
        }
....
}
  1. 更新 Activity 以开始接收事件:

public class TaxiMapActivity extends MapActivity{

BarrePersonnalisee barre;

...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);

}

@Subscribe(threadMode = ThreadMode.MAIN)
公共无效onBarrePersonnaliseeEvent(BarrePersonnaliseeEvent事件){
Toast.makeText(getActivity(), "触摸按钮:" + event.isTouchDown(), Toast.LENGTH_SHORT).show();
}

}

  1. 确保在您的 Activity 中注册和取消注册 EventBus

    @覆盖
    公共无效onStart(上下文上下文){
    super.onStart(上下文);
    EventBus.getDefault().register(this);
    }

    @覆盖
    公共无效onStop(){
    super.onStop();
    EventBus.getDefault().unregister(this);
    确保

You can actually use EventBus here: http://greenrobot.org/eventbus/

  1. Load the EventBus

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:support-v4:24.1.1'
    compile 'org.greenrobot:eventbus:3.0.0'
    }

  2. Create new Event type Class. Let's call it BarrePersonnaliseeEvent.

public class BarrePersonnaliseeEvent {

private boolean _touchDown;
public BarrePersonnaliseeEvent(boolean touchDown) {
    _touchDown = touchDown;
}

public boolean isTouchDown(){
    return _touchDown;
} }
  1. Now pass the event to the EventBus

public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
Context mycontext;
View convertview;
ImageButton searchadresse;

    public BarrePersonnalisee(Context context, AttributeSet attrs) {
        super(context, attrs);
        mycontext=context;
        convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
        searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
        searchadresse.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.searchadresse:
            //I want to dispatch this event
                EventBus.getDefault().post(new BarrePersonnaliseeEvent(true));
                break;
               }
        }
....
}
  1. Update Activity to start receiving events:

public class TaxiMapActivity extends MapActivity{

BarrePersonnalisee barre;

...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);

}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onBarrePersonnaliseeEvent(BarrePersonnaliseeEvent event) {
Toast.makeText(getActivity(), "Button touched: " + event.isTouchDown(), Toast.LENGTH_SHORT).show();
}

}

  1. Make sure to register and unregister for EventBus in your Activity

    @Override
    public void onStart(Context context) {
    super.onStart(context);
    EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文