想要在以后的日子里将一些经典基础的代码记录下来,方便自己学习。此文章会不定时更新。
一、数组排序操作
package com.shuzu;
public class Ranking {
public static void main(String[] args) {
int data[]=new int []{1,4,5,6,7,9,0,8};
sort(data);//排序方法
print(data);//输出数组
}
private static void sort(int[] data) {
for(int x=0;x<data.length;x++){ //控制排序次数
for(int y=0;y<data.length-1;y++){ //排序数组
if(data[y]>data[y+1]){
int t=data[y]; //交换数据
data[y]=data[y+1]; //交换数据
data[y+1]=t; //交换数据
}
}
}
}
private static void print(int[] data) {
for(int x=0;x<data.length;x++){
System.out.print(data[x]+"、");
}
System.out.println ();
}
}
利用java.util.Arrays.sort(数组)完成排序的代码:
/*
* 方法二,利用Arrays.sort(数组)
*/
public static void main(String[] args) {
int data[]=new int []{1,4,5,6,7,9,0,8};//定义数组
Arrays.sort(data);//数组排序
print(data);
}
private static void print(int[] data) {
for(int x=0;x<data.length;x++){
System.out.print(data[x]+"、");
}
System.out.println();
}
}
二、实现登录背景动画效果
public class LoginActivity extends AppCompatActivity {
private ImageView mBgPic;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
loadAnim();
}
private void loadAnim() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Animation animation = AnimationUtils.loadAnimation(LoginActivity.this,R.anim.translate_anim);
mBgPic.startAnimation(animation);
}
},200);
}
private void initView(){
mBgPic = (ImageView) findViewById(R.id.iv_bg_pic);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_bg_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="-100dp"
android:layout_marginRight="-160dp"
android:scaleType="centerCrop"
android:src="@drawable/pic_2" />
</FrameLayout>
<LinearLayout
android:id="@+id/ll_input_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="100dp"
android:orientation="vertical">
<EditText
android:id="@+id/et_input_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/phone"
android:gravity="center|left"
android:hint="请输入手机号"
android:text="" />
<EditText
android:id="@+id/et_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/password"
android:gravity="center|left"
android:hint="请输入密码"
android:text="" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_input_area"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="登录" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_login"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="新用户"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/btn_login"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="忘记密码"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
三、实现友好的用户输入界面(登录&注册)
xml布局如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp" />
<!--android:inputType="numberPassword"表明只能接受数字密码-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="16sp" />
<!--android:inputType="number"表明是数值输入框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp" />
<!--android:inputType="date"表明是日期输入框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="号码:"
android:textSize="16sp" />
<!--android:inputType="phone"表明是电话号码输入框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:selectAllOnFocus="true" />
</TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</TableLayout>
Java代码:
/**
* Created by zsf on 2017/2/5.
* 一个友好的用户输入界面原则:
* 1.接受用户输入的文本框应该有提示信息;
* 2.当用户把焦点切换到输入框时,输入框自动选中其中已经输入的内容,避免用户删除已有的内容;
* 3.当用户把焦点切换到只接受电话号码的输入框时,输入法自动切换到数字键盘
*/
public class EditTextTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_input);
}
}