纯Java代码写登录注册页面

package com.honor.demo;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

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

        initUI();
    }



    public final void initUI(){
        ScrollView main = new ScrollView(this);
        main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        main.setBackgroundColor(Color.WHITE);

        //根布局参数
        LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
        layoutParamsRoot.gravity = Gravity.CENTER;
        //根布局
        LinearLayout layoutRoot = new LinearLayout(this);
        layoutRoot.setLayoutParams(layoutParamsRoot);
        layoutRoot.setOrientation(LinearLayout.VERTICAL);


        //上边距(dp值)
        int topMargin = dip2px(this, 30);
        //imageMain宽度(dp值)
        int widthMain = dip2px(this, 240);
        //imageMain高度(dp值)
        int heightMain = dip2px(this, 120);

        //imageMain布局参数
        LinearLayout.LayoutParams layoutParamsImageMain = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//        LinearLayout.LayoutParams layoutParamsImageMain = new LinearLayout.LayoutParams(widthMain,heightMain);
        layoutParamsImageMain.topMargin = topMargin;
        layoutParamsImageMain.bottomMargin = topMargin;
        layoutParamsImageMain.leftMargin = topMargin;
        layoutParamsImageMain.rightMargin = topMargin;
        layoutParamsImageMain.gravity=Gravity.CENTER_HORIZONTAL;
        //初始化ImageView
        ImageView imageMain = new ImageView(this);
        imageMain.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageMain.setAdjustViewBounds(true);
        imageMain.setBackgroundColor(Color.BLACK);
        imageMain.setImageResource(R.mipmap.ic_launcher);
        layoutRoot.addView(imageMain, layoutParamsImageMain);

        //textInfo布局参数
        LinearLayout.LayoutParams layoutParamsTextInfo = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        layoutParamsTextInfo.topMargin = topMargin;
        layoutParamsTextInfo.bottomMargin = topMargin;
        layoutParamsTextInfo.leftMargin = topMargin;
        layoutParamsTextInfo.rightMargin = topMargin;
        layoutParamsTextInfo.gravity=Gravity.CENTER_HORIZONTAL;
        //初始化textInfo
        TextView textInfo = new TextView(this);
        textInfo.setGravity(Gravity.CENTER_HORIZONTAL);
        textInfo.setTextSize(18);
        layoutRoot.addView(textInfo, layoutParamsTextInfo);

        //editInfo布局参数
        LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//        LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(widthMain,LayoutParams.WRAP_CONTENT);
        layoutParamsEditInfo.topMargin = topMargin;
        layoutParamsEditInfo.gravity=Gravity.CENTER_HORIZONTAL;
        //初始化editInfo
        EditText editInfo = new EditText(this);
        editInfo.setHint("Please enter your mobile number");
        //设置可输入的最大长度
        InputFilter[] filters = {new InputFilter.LengthFilter(200)};
        editInfo.setFilters(filters);
        editInfo.setTextSize(18);
        layoutRoot.addView(editInfo, layoutParamsEditInfo);

        //上边距(dp值)
        int minHeight = dip2px(this, 54);
        //上padding(dp值)
        int topPadding = dip2px(this, 4);
        //左padding(dp值)
        int leftPadding = dip2px(this, 2);
        //按钮布局
        LinearLayout layoutButton = new LinearLayout(this);
        layoutButton.setLayoutParams(layoutParamsEditInfo);
        layoutButton.setOrientation(LinearLayout.HORIZONTAL);
//        layoutButton.setBackgroundColor(Color.parseColor("#c6c3c6"));
        layoutButton.setMinimumHeight(minHeight);
        layoutButton.setPadding(leftPadding, topPadding, leftPadding, topPadding);
        int layoutButtonId=View.generateViewId();
        layoutButton.setId(layoutButtonId);

        //buttonOK布局参数
        LinearLayout.LayoutParams layoutParamsButtonOK = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        layoutParamsButtonOK.gravity = Gravity.LEFT;
        layoutParamsButtonOK.leftMargin = dip2px(this, 10);
        layoutParamsButtonOK.rightMargin = dip2px(this, 5);
        layoutParamsButtonOK.weight = 1;
        //Button确定
        Button buttonOK = new Button(this);
        buttonOK.setLayoutParams(layoutParamsButtonOK);
        buttonOK.setMaxLines(2);
        buttonOK.setTextSize(18);
        buttonOK.setBackgroundColor(Color.parseColor("#FF0000"));
        buttonOK.setTextColor(Color.parseColor("#FFFFFF"));
        buttonOK.setText("OK");
        layoutButton.addView(buttonOK);
        buttonOK.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Toast.makeText(getApplicationContext(),"---ok---",Toast.LENGTH_SHORT).show();
            }
        });

        //buttonCancel布局参数
        LinearLayout.LayoutParams layoutParamsButtonCancel = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParamsButtonCancel.gravity = Gravity.RIGHT;
        layoutParamsButtonCancel.leftMargin = dip2px(this, 5);
        layoutParamsButtonCancel.rightMargin = dip2px(this, 10);
        layoutParamsButtonCancel.weight = 1;
        //Button取消
        Button buttonCancel = new Button(this);
        buttonCancel.setLayoutParams(layoutParamsButtonCancel);
        buttonCancel.setMaxLines(2);
        buttonCancel.setTextSize(18);
        buttonCancel.setBackgroundColor(Color.parseColor("#F2F2F2"));
        buttonCancel.setTextColor(Color.parseColor("#242424"));
        buttonCancel.setText("Cancel");

        layoutButton.addView(buttonCancel);

        layoutRoot.addView(layoutButton, layoutParamsEditInfo);

        //RelativeLayout布局参数
        LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        RelativeLayout layoutBottom = new RelativeLayout(this);
        layoutBottom.setLayoutParams(layoutParamsBottom);

        RelativeLayout.LayoutParams paramsImageBottom = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        paramsImageBottom.addRule(RelativeLayout.BELOW, layoutButtonId);
        paramsImageBottom.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        paramsImageBottom.setMargins(topMargin, topMargin, topMargin, topMargin);

/*        //初始化ImageView
        ImageView imageBottom = new ImageView(this);
        imageBottom.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageBottom.setAdjustViewBounds(true);
        imageBottom.setBackgroundColor(0xFF777777);
        imageBottom.setImageResource(android.R.drawable.ic_dialog_email);
        layoutBottom.addView(imageBottom, paramsImageBottom);
        layoutRoot.addView(layoutBottom);*/


        //TODO TEST
//		imageMain.setBackgroundResource(android.R.drawable.ic_dialog_map);
        textInfo.setText("Create "+getApplicationContext().getString(R.string.app_name)+" Account");

        main.addView(layoutRoot);
        setContentView(main);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值