Android开发探秘之一:创建可以点击的Button

Android开发探秘之一:创建可以点击的Button,第1张

概述Android开发探秘之一:创建可以点击的Button

感觉到自己有必要学习下手机开发方面的知识,不论是为了以后的工作需求还是目前的公司项目。

当然,任何新东西的开始,必然伴随着第一个HelloWorld,AndroID学习也不例外。既然才开始,我就不做过多的描述了。

对于AndroID开发的IDE:ADT来说,打开的第一眼有点迷糊,不过看了网上各种目录结构的介绍,慢慢的就明白了,做这个实例,我们尤其需要关注两个地方,一个是src目录,一个就是res目录下的layout目录。src目录放置的是code-behind源码,而layout目录放置的则是xml前台配置文件。

既然我们要实现的功能是点击按钮,然后EditText中显示“Hello World!”。让我们先打开layout文件,拖放一个button上去,然后拖放一个EditText上去,最后的xml文件结构如下:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:paddingBottom="@dimen/activity_vertical_margin"    androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <button        androID:ID="@+ID/button1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:clickable="true"        androID:text="button" />    <EditText        androID:ID="@+ID/editText1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignBaseline="@+ID/button1"        androID:layout_alignBottom="@+ID/button1"        androID:layout_toRightOf="@+ID/button1"        androID:text="EditText" /></relativeLayout>

然后在后台代码文件中,我们需要引入两个命名空间:

import androID.Widget.button;import androID.Widget.EditText;

全部代码如下:

package com.example.helloworld;import androID.os.Bundle;import androID.app.Activity;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;public class MainActivity extends Activity {    private button mybutton;    private EditText myText;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);                mybutton = (button)findVIEwByID(R.ID.button1);        myText = (EditText)findVIEwByID(R.ID.editText1);                mybutton.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                // Todo auto-generated method stub                myText.setText("Hello World!");            }        });    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

然后点击运行按钮,在虚拟机界面中点击按钮,得到的结果如下图:

这节就到这里了,下面让我们继续探秘吧。

总结

以上是内存溢出为你收集整理的Android开发探秘之一:创建可以点击的Button全部内容,希望文章能够帮你解决Android开发探秘之一:创建可以点击的Button所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1119230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存