如何使用的OnClick在android系统编程 [英] How to use OnClick in android programming

查看:104
本文介绍了如何使用的OnClick在android系统编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全新的节目,我想创建一个Android应用程序。此刻我想了解如何使用的OnClick()来使它所以我对MainActivity链接到另一个活动按钮。

I am completely new to programming and I am trying to create an android app. At the moment I am trying to understand how to use OnClick() to make it so my button on MainActivity links to another activity.

推荐答案

有关执行以下步骤上按钮单击一些行动:

For doing some Action on Button Click following these Steps :

第1步:

在你的活动布局添加一个按钮:

Add a button in your Activity layout as:

<Button
    android:id="@+id/button_id_here"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>



第2步:

添加您的 NextActivity 的Andr​​oidManifest.xml 为:

 <!-- your other xml  -->
 <application
  <!-- your other xml  -->
        <activity
            android:name=".NextActivity" />

 </application>



第3步:

在MainActivity code添加点击一个按钮监听器button_id_here为:

In MainActivity code add a Button click Listener to button_id_here as:

public class MainActivity extends Activity {

    Button button_test;  //<< Create Button instance here
    Intent intent;       //<< For starting new Activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

            // Add layout to Activity here
        setContentView(R.layout.your_Activity_layout);

         // Initilie button here
         button_test= (Button) findViewById(R.id.button_id_here);

               // add a onclick listner to button here
         button_test.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
              intent = new Intent(MainActivity.this,NextActivity.class);

              startActivity(intent); //<<< start Activity here
            }
        });
     }

}


如果仍面临上做按钮点击某个活动,然后在这里学到的:


if still facing to do some activity on Button click then learn here:

https://developer.android.com/training/index.html

这篇关于如何使用的OnClick在android系统编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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