zoukankan      html  css  js  c++  java
  • Android APP开发入门教程-Button 分类: JAVA 2015-01-15 14:25 360人阅读 评论(0) 收藏

    代码编写

    做好准备工作后,终于可以开始写我们的hello android了,在开始编写代码之前,我们先了解几个文件:

    res/layout/main.xml App主窗体布局文件,你的应用长什么样都在这边定义,有Design和Text两种模式

    res/values/strings.xml 可以理解为i18n文件,这个文件用来存放程序调用的各种字符串

    src/com/example/helloandroid/MyActivity.java 这个就是我们的主程序类,等下要实现的功能都在这个文件里添加

    首先为应用添加一个id为hellotextView的textview和一个id为hellobutton的button,mail.xml 代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="180dp"
                android:text="@string/default_message"
                android:id="@+id/hellotextView" android:textColor="#00ff00" android:gravity="center"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_send"
                android:id="@+id/hellobutton" android:layout_gravity="center"/>
    </LinearLayout>


    代码和控件用到的字符串定义如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">helloandroid by hiwanz</string>
        <string name="button_send">Say something</string>
        <string name="default_message">Click button below!</string>
        <string name="interact_message">You just clicked on the Button!</string>
    </resources>



    主程序中定义button点击后改变textview显示的文本,并且弹出Toast提示信息,代码如下:

    package com.example.helloandroid;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MyActivity extends Activity {
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //得到按钮实例
            Button hellobtn = (Button)findViewById(R.id.hellobutton);
            //设置监听按钮点击事件
            hellobtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //得到textview实例
                    TextView hellotv = (TextView)findViewById(R.id.hellotextView);
                    //弹出Toast提示按钮被点击了
                    Toast.makeText(MyActivity.this,"Clicked",Toast.LENGTH_SHORT).show();
                    //读取strings.xml定义的interact_message信息并写到textview上
                    hellotv.setText(R.string.interact_message);
                }
            });
        }
    
    }
    


    代码写好后,电脑通过USB数据线连接手机,手机系统设置里的开发人员选项里打开USB调试,在IDE中直接点Run就可以在手机上看到运行的效果了。

    helloandroid-1

    helloandroid-2

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    解析大型.NET ERP系统 权限模块设计与实现
    Enterprise Solution 开源项目资源汇总 Visual Studio Online 源代码托管 企业管理软件开发框架
    解析大型.NET ERP系统 单据编码功能实现
    解析大型.NET ERP系统 单据标准(新增,修改,删除,复制,打印)功能程序设计
    Windows 10 部署Enterprise Solution 5.5
    解析大型.NET ERP系统 设计异常处理模块
    解析大型.NET ERP系统 业务逻辑设计与实现
    解析大型.NET ERP系统 多国语言实现
    Enterprise Solution 管理软件开发框架流程实战
    解析大型.NET ERP系统 数据审计功能
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706460.html
Copyright © 2011-2022 走看看