Android入门

一、Android开发之环境准备;

1. 安装JDK;

2. 安装Eclipse;

3. 安装SDK;

首先是配置环境变量,具体方式访问:https://jingyan.baidu.com/article/f71d603757965b1ab641d12a.html

4. 在Eclipse上安装Android开发插件;

这是一个ADT包,我一开始的时候用的是ADT22.3.0但是安装了之后出现与SDK版本冲突等问题,新建不了Android application project。解决方法就是换成ATD23.0.0之后的版本,问题就能解决。

5. 配置Eclipse的Android开发环境;
 个可执行文件。Android入门Android入门

该程序可以下载更新不同的Android系统,一开始我下的是Android8.0,当时Eclipse并不支持,其页面布局的.xml文档在eclipse里完全显示不了。之后解决方法就是下载Android较低的版本,我下的就是Android4.4.2,这样就可以了。

要想在电脑上运行Android项目,就必须使用AVD manger,这是和SDK manger在统一目录下的可执行文件。Android入门

这是用来创建虚拟机的程序,通过这个程序可以在电脑上运行一个Android系统的虚拟机,但是这个程序必须要有加速器程序Haxm,而且还得是6.0及之后的版本才可以。有的电脑已经有了再开启虚拟机的时候就不会报错。没有的需要在官网上下载一份直接安装后就可以解决问题。下载地址为:https://software.intel.com/zh-cn/android/articles/intel-hardware-accelerated-execution-manager

二、Android开发重要部分;

1. Activity;

这是用来处理页面数据的部分,是继承Activity类。每一个Activity类都会绑定一个Layout内的一个XML文档,该文档就是显示在Android手机上的页面布局。这些类都是放在src文件夹下的。

2. Layout;

在该文件夹下放置的时所有APP的页面文件,格式都是.xml格式。在创建activity的同事Eclipse会自动创建一个.xml 文件放在layout文件夹下;

3. AndroidManifest.xml;

这是用在存储已注册的Activity的文件。所有的Activity类要想正常使用就必须得在这个文件内注册,注册格式如下:

三、Android程序实例;

1. Activity类;

 

package com.example.study;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	public void submit(View view)
	{
		EditText name=(EditText)this.findViewById(R.id.name);
		EditText pass=(EditText)this.findViewById(R.id.password);
		String user=name.getText().toString().trim();
		String password=pass.getText().toString().trim();
		if(user.equals("chen")&&password.equals("123456"))
		{
			Intent intent=new Intent();  
            intent.setClass(MainActivity.this,Home.class);  
            MainActivity.this.startActivity(intent); 
		}
		else
		{
			Toast toast=Toast.makeText(this, user+"用户名不存在或密码错误"+password, 5);
			toast.show();
		}
		
	}
}

 

package com.example.study;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class Home extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

2. Layout布局文件;

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    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="com.example.study.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="输入账号密码:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:text="姓名:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/textView2"
        android:inputType="textPersonName" >

    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/name"
        android:layout_marginTop="30dp"
        android:text="密码"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/name"
        android:layout_alignRight="@+id/name"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="submit"
        android:text="登录" />

</RelativeLayout>

 

<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="com.example.study.Home" >

    <TextView
 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Welcome To Our World" />

</RelativeLayout>

 

3. 效果展示;

当出现登录密码错误时,系统提示错误信息;
Android入门

当账号密码都正确匹配后,界面跳到另一个界面。
 
Android入门
 

 

相关推荐