【Android】2:控件使用及页面跳转实战
来源:互联网 发布:韩式四宫格拼图软件 编辑:程序博客网 时间:2024/05/16 15:49
在搭建了开发环境之后,我们就要就开始了解,如何写程序了。首先我们先来看看Eclipse开发环境的各个窗口布局,如下图所示
其实这个和当初接触的VB开发界面类似,我们可以将控件列表里面的控件拖进我们的页面布局中,也就是我们主要的工作区,然后在使用控件的各种属性,调整大小和位置,设置外观和颜色,即可完成页面布局设计。然后为各个控件及事件,编写后台代码,调试保证没错,一个应用程序就开发完成了。是不是特别easy?下面来看一个小例子,体会一下Android开发的便捷之处。
新建一个AndroidApplication,然后在再新建一个Android XML文件,就是我们所说的页面暂且叫做activity-game,同时新建一个Activity类GameActivity,用于控制activity-game页面。我们要实现的是在第一个页面放一个textview和一个button,为按钮加上一个事件,跳转到下一个页面activity-game,就这么简单,在这个例子中主要要体会如何给各个控件设置属性,使之比较“顺眼”。
具体的例子如下:
activity-main.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:background="@drawable/jiemian" 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="com.example.myapp.MainActivity" > <Button android:id="@+id/button1" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="98dp" android:background="#56C10F" android:text="进入游戏" android:textColor="#FF7800" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="51dp" android:text="这是一款很牛逼的手游!" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#008A9D" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignLeft="@+id/autoCompleteTextView1" android:layout_marginBottom="36dp" android:background="#E5E9E8" android:ems="10" android:hint="请输入账号……" android:inputType="textPassword" /> <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginBottom="29dp" android:background="#E5E9E8" android:ems="10" android:hint="请输入密码……" > <requestFocus /> </AutoCompleteTextView></RelativeLayout>
MainActivity.java的代码如下:
public class MainActivity extends ActionBarActivity implements OnClickListener {private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button =(Button)findViewById(R.id.button1); button.setOnClickListener(this); }@Overridepublic void onClick(View arg0) {if(arg0.getId()==R.id.button1){Intent intent = new Intent();intent.setClass(MainActivity.this, GameActivity.class);startActivity(intent);}}}
activity-game.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/jiemian" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="190dp" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="134dp" android:textColor="#3266CC" android:text="天下风云出我辈,\n一入江湖岁月催;\n皇图霸业谈笑中,\n不胜人生一场醉。" android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>
GameActivity.java的代码如下:
public class GameActivity extends Activity implements OnClickListener {private Button button;/* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_game);}@Overridepublic void onClick(View arg0) {}}
最后我们来看运行结果,从手机上截的图,如下:
小结一下:
一开始刚接触Android的时候,觉得挺简单的啊,跟VB差不多嘛,无非就是妥妥拽拽,写点事件代码即可,可是当我学到后面关于Handler和Thread这方面的内容时,又觉得怎么这么难呢?可能是接触的还少,也可能是在之前的学习中没没有对线程和Handler这类的东西进行过总结,所以在Android中看到类似的内容时不那么感冒,因此就觉得难,不过在做了几个实战例子之后,能够很好的理解了Handler+Thread的作用了。
- 【Android】2:控件使用及页面跳转实战
- Android 开发实战 控件设置及页面跳转
- Android 实战开发 页面跳转数据传递
- Android页面跳转Intent使用
- android-页面跳转及携带数据
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- Android 页面跳转传递参数及页面返回接收参数
- Android入门--项目中添加页面及页面跳转
- android使用SharedPreferences来跳转不同页面
- Android使用Intent实现页面跳转
- 使用 uilabel等 制作的登陆页面 及页面跳转
- iOS使用StroryBoard页面跳转及传值 (转载)
- iOS使用StroryBoard页面跳转及传值
- Android App欢迎页面(2秒后跳转)及广告页面、节日动画(3秒后消失)
- android 关于页面跳转
- Android页面跳转
- android基础页面跳转
- android基础页面跳转
- 语音识别-关键词检测
- ubuntu启动非常慢,报device descriptor read/64, error -110 的解决办法
- java多线程(学习笔记)
- 黑马程序员——javaSE_网络编程
- 决策树入门篇
- 【Android】2:控件使用及页面跳转实战
- 元素类型为 "class" 的内容必须匹配 "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),di
- 图文详细解决office2010弹出:“此错误通常是由宏安全 性设置造成。。。。”窗口的问题
- ORM框架
- Form(窗体)的FormBorderStyle属性的不同效果
- 安装卸载openjdk&&sunjdk
- javaSE(11)(集合迭代器与比较器)
- hpuoj--校赛--特殊的比赛日期(素数判断+模拟)
- ThinkPHP框架二 框架配置