笔者最近学了一点安卓开发,装好环境之后,想上网找一找有木有入门的小案例,能让我快速了解安卓开发的大概流程,如布局文件在哪,怎么引入;java类写在哪,如何声明等等一系列特别提别基础的东西。找了好久,发现网上的东西零零散散,竟没能找到一个能让我一看就懂的入门Demo。无奈之后,结合网上的资料,自己研究吧,不会自学的程序猿不是好码农-. -
开发流程:
1.搭环境
搭安卓开发环境不用我多赘述,网上到处都是。大概就是先配置JDK,再配置SDK,最后下载谷歌的Android Studio;或者先配置JDK,再下载Android Studio,然后在Android Studio的引导下下载配置SDK,再在Android Studio的引导下下载相对版本的Grandle。最后去settings中分别将sdk与Grandle配置等等。
2.新建工程的目录
安卓的工程目录如图一所示:主要提了一下咱们开发中用到的,入门用的,其他的文件想研究自己了解。
图一
3.具体开发流程
需求:绘制两个界面,然后点击确定按钮,将第一个页面的值,传到第二个页面。
(1)在layout文件下创建“huanghe_bank.xml”绘制demo的主界面,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:src="@mipmap/bank"
android:layout_centerVertical="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background"
android:orientation="vertical">
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:hint="请输入11位手机号码"
android:selectAllOnFocus="true"
android:singleLine="false"
android:textColorHint="#95A1AA" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="10dp"
android:hint="请输入密码"
android:selectAllOnFocus="true"
android:singleLine="false"
android:textColorHint="#95A1AA" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_gravity="right"
android:layout_marginEnd="32dp"
android:text="忘记密码?"
android:textColor="#4C91E3"
android:textSize="18dp" />
</RelativeLayout>
<Button
android:id="@+id/button_confirm"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:background="#C42C2C"
android:text="确定"
android:textColor="#fff"
android:textSize="22dp" />
<TextView
android:id="@+id/tv_regist"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="right"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="还没有账号,立即注册"
android:textColor="#aaaaaa"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:gravity="center"
android:text="切换登录方式"
android:textColor="#000"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
界面如图:
(1)在layout文件下创建“bank_second.xml”绘制demo的主界面,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Hello HuangHe Bank !"
android:gravity="center"
android:textColor="#000"
android:textSize="20dp"
/>
<TextView
android:id="@+id/text_phone"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text=""
android:textColor="#0f9d0a"
android:textSize="20dp"
/>
<TextView
android:id="@+id/text_pwd"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text=""
android:textColor="#0f9d0a"
android:textSize="20dp"
/>
</LinearLayout>
界面如图:
(3)页面画好了,这时候要在layout-activity_main.xml中引入你的app要显示的默认界面,就是咱们写的第一个页面。
接下来就可以在手机中运行看到界面效果了,但是只能看到第一个页面,而且不能跳转。接下来咱开始用java代码控制页面让页面动起来。
(4)在java文件夹下的MainActivity中(工程应该帮你建好了)开始写逻辑。值得提醒新手的是,你要对某个控件进行操作,一定要
在布局页面中给控件起个id名字。
这样才能对控件进行操作。java代码如下:
package com.example.huanghebank;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// private String message ="还没有账号,立即注册";
private TextView tv; //定义一个文本框控件
private Button btn;
private EditText et_pwd;
private EditText et_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*自定义代码*/
// 让文字注册变色
tv = findViewById(R.id.tv_regist);
tv.setText(Html.fromHtml(message));
// 添加点击事件
// tv.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// String pwd = et_pwd.getText().toString();
// String phone =et_phone.getText().toString();
// Intent intent = new Intent();
// intent.setClass(MainActivity.this,ThirdActivity.class);
// startActivity(intent);
// }
// });
et_pwd = findViewById(R.id.et_pwd);
et_phone = findViewById(R.id.et_phone);
// 给确认按钮添加一个点击事件
btn = findViewById(R.id.button_confirm);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pwd = et_pwd.getText().toString();
String phone =et_phone.getText().toString();
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
intent.putExtra("password",pwd);
intent.putExtra("phone",phone);
startActivity(intent);
}
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
以上实现了给安卓界面中写了一句话:“还没有账号,立即注册”。还实现了拿到文本框输入的phone和password,给确认按钮添加了点击事件,
这几句代码是实现用intent对象告诉安卓你要从这个activity跳转到第二个activity(咱们还没定义,一会定义),并将参数携带过去。
(5)在java文件夹下创建另外一个java类SecondActivity。注意:要在AndroidManifest.xml定义你创建的这个类,系统会帮你创建好MainActivity,但是不会帮你创建你自定义的。
<activity
android:name="com.example.huanghebank.SuccessActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
(6)SecondActivity代码如下:
package com.example.huanghebank;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView tv_pwd; //定义一个文本框控件
private TextView tv_phone; //定义一个文本框控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bank_second);
tv_pwd = findViewById(R.id.text_pwd);
tv_phone = findViewById(R.id.text_phone);
String password = getIntent().getStringExtra("password");
String phone = getIntent().getStringExtra("phone");
tv_pwd.setText(password);
tv_phone.setText(phone);
}
}
重写onCreate方法,并告诉安卓,你要渲染bank_second这个页面。
(5)运行结果:输入电话号和密码,点击确定,会跳转到第二个页面,在第二个页面会将你输入的电话号和密码显示出来,So easy!
4.总结
大概步骤和值得注意的点上文我应该都提到了,大家做的时候要多尝试,多思考,入门最重要,万事开头难,入了门,一切都变得简单无比,大家加油,共同学习!