Android的按钮onClickListener [英] Android button onClickListener

查看:125
本文介绍了Android的按钮onClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发。我试图打开新的活动在我的 OnClickListener 方法。我应该怎么写,怎么做 OnClickListener 法的工作?

I am new to Android Development. I am trying to open new Activity in my OnClickListener method. What should i write and how does OnClickListener method work?

推荐答案

这个任务可以使用一个命名为意图的Andr​​oid的主要组成部分和一个方法公共无效startActivity(意向意图来完成)属于你的Activity类。

This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.

这是意图是要执行的操作的抽象描述。它可以与startActivity被用来发起活动,broadcastIntent将其发送给任何感兴趣的BroadcastReceiver的成分,和startService(意向)或bindService(意向,ServiceConnection,INT)与后台服务进行通信。

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

这是意图提供了一个工具,以便进行后期运行时的code在不同的应用结合。其最显著的用途是在活动中,在那里它可以被认为是活动间的粘合剂的启动。它基本上是要执行一个被动的数据结构保持一个动作的抽象描述。

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

请参考官方文档 - <一个href="http://developer.android.com/reference/android/content/Intent.html">http://developer.android.com/reference/android/content/Intent.html

Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html

公共无效startActivity(意向意图) - 用来启动一个新的活动

public void startActivity (Intent intent) -- Used to launch a new activity.

因此​​,假设你有两个Activity类 -

So suppose you have two Activity class --

  1. presentActivity - 这是你想要去的第二项活动您当前的活动

  1. PresentActivity -- This is your current activity from which you want to go the second activity.

NextActivity - 这是您要在其移动下一个活动

NextActivity -- This is your next Activity on which you want to move.

因此​​,意图会是这样

So the Intent would be like this

Intent(PresentActivity.this, NextActivity.class)

最后,这将是完整的code

Finally this will be the complete code

  public class PresentActivity extends Activity {
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            setContentView(R.layout.content_layout_id);

            final Button button = (Button) findViewById(R.id.button_id);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

                    Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);

                    // currentContext.startActivity(activityChangeIntent);

                    PresentActivity.this.startActivity(activityChangeIntent);
                }
            });
        }
    }

我希望你现在能理解,如果你正面临的任何问题,回到我这里只。总是乐于帮助。

I hope you can understand now and If you are facing any problem get back to me here only. Always happy to help.

这篇关于Android的按钮onClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆