经典的代码案例

想要在以后的日子里将一些经典基础的代码记录下来,方便自己学习。此文章会不定时更新。


一、数组排序操作

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







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值