Android使用表格布局設(shè)計注冊界面
Android中使用表格布局設(shè)計注冊界面,供大家參考,具體內(nèi)容如下
注冊頁面包含:用戶名,密碼,確認(rèn)密碼,驗證碼。驗證碼使用一個封裝好的工具類生成的,可以直接使用。效果圖如下所示。沒有做事件處理,僅僅做了個簡單的表格布局。
XML布局的代碼如下:
<?xml version="1.0" encoding="utf-8"?> <TableLayout 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:stretchColumns="1" tools:context="com.example.nuist__njupt.register.MainActivity"> <TextView android:id="@+id/register_title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="用戶注冊" android:textSize="20dp" android:gravity="center_horizontal" android:textColor="@android:color/black" android:layout_marginTop="5dp" /> <TableRow> <TextView android:id="@+id/user_name" android:text="用戶名" android:textSize="15dp" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:textColor="@android:color/black" /> <EditText android:layout_width="match_parent" android:id="@+id/input_name" android:hint="請輸入用戶名" android:textSize="15dp" android:layout_marginTop="20dp" android:selectAllOnFocus="true" android:layout_span="2" /> </TableRow> <TableRow> <TextView android:id="@+id/user_password" android:text="密碼" android:textSize="15dp" android:layout_marginLeft="5dp" android:textColor="@android:color/black" /> <EditText android:layout_width="match_parent" android:id="@+id/input_password" android:hint="請輸入密碼" android:textSize="15dp" android:inputType="textPassword" android:layout_span="2" /> </TableRow> <TableRow> <TextView android:id="@+id/confirm_password" android:text="確認(rèn)密碼" android:textSize="15dp" android:layout_marginLeft="5dp" android:textColor="@android:color/black" /> <EditText android:layout_width="match_parent" android:id="@+id/input_confirm" android:hint="請再次輸入密碼" android:textSize="15dp" android:inputType="textPassword" android:layout_span="2" /> </TableRow> <TableRow> <TextView android:id="@+id/test_password" android:text="驗證碼" android:textSize="15dp" android:layout_marginLeft="5dp" android:textColor="@android:color/black" /> <EditText android:layout_width="match_parent" android:id="@+id/input_test" android:hint="請輸入驗證密碼" android:textSize="15dp" android:layout_toLeftOf ="@+id/image" /> <ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="40dp"/> </TableRow> <Button android:id="@+id/register" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="注冊" android:textColor="@android:color/black" android:textSize="15dp" android:gravity="center_horizontal" /> <Button android:id="@+id/cancel" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="取消" android:textColor="@android:color/black" android:textSize="15dp" android:gravity="center_horizontal" /> </TableLayout>
java代碼如下:
public class MainActivity extends AppCompatActivity { private Bitmap bitmap; private String code; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageView image = (ImageView) findViewById(R.id.image); //獲取需要展示圖片驗證碼的ImageView bitmap = CodeUtils.getInstance().createBitmap(); //獲取工具類生成的圖片驗證碼對象 code = CodeUtils.getInstance().getCode(); //獲取當(dāng)前圖片驗證碼的對應(yīng)內(nèi)容用于校驗 image.setImageBitmap(bitmap); image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { bitmap = CodeUtils.getInstance().createBitmap(); code = CodeUtils.getInstance().getCode(); image.setImageBitmap(bitmap); Toast.makeText(MainActivity.this, code, Toast.LENGTH_SHORT).show(); //圖片驗證碼內(nèi)容提示 } }); } }
工具類CodeUtils的代碼如下:
/** * Created by nuist__NJUPT on 2021/4/16. */ public class CodeUtils { //隨機(jī)碼集 private static final char[] CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; private static CodeUtils mCodeUtils; private int mPaddingLeft, mPaddingTop; private StringBuilder mBuilder = new StringBuilder(); private Random mRandom = new Random(); //Default Settings private static final int DEFAULT_CODE_LENGTH = 4;//驗證碼的長度 這里是4位 private static final int DEFAULT_FONT_SIZE = 60;//字體大小 private static final int DEFAULT_LINE_NUMBER = 3;//多少條干擾線 private static final int BASE_PADDING_LEFT = 20; //左邊距 private static final int RANGE_PADDING_LEFT = 30;//左邊距范圍值 private static final int BASE_PADDING_TOP = 70;//上邊距 private static final int RANGE_PADDING_TOP = 15;//上邊距范圍值 private static final int DEFAULT_WIDTH = 200;//默認(rèn)寬度.圖片的總寬 private static final int DEFAULT_HEIGHT = 100;//默認(rèn)高度.圖片的總高 private static final int DEFAULT_COLOR = Color.rgb(0xee, 0xee, 0xee);//默認(rèn)背景顏色值 private String code; public static CodeUtils getInstance() { if (mCodeUtils == null) { mCodeUtils = new CodeUtils(); } return mCodeUtils; } //生成驗證碼圖片 public Bitmap createBitmap() { mPaddingLeft = 0; //每次生成驗證碼圖片時初始化 mPaddingTop = 0; Bitmap bitmap = Bitmap.createBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); code = createCode(); canvas.drawARGB(0, 0, 0, 0); canvas.drawColor(DEFAULT_COLOR); Paint paint = new Paint(); paint.setTextSize(DEFAULT_FONT_SIZE); for (int i = 0; i < code.length(); i++) { randomTextStyle(paint); randomPadding(); canvas.drawText(code.charAt(i) + "", mPaddingLeft, mPaddingTop, paint); } //干擾線 for (int i = 0; i < DEFAULT_LINE_NUMBER; i++) { drawLine(canvas, paint); } canvas.save();//保存 canvas.restore(); return bitmap; } /** * 得到圖片中的驗證碼字符串 * * @return */ public String getCode() { return code; } //生成驗證碼 public String createCode() { mBuilder.delete(0, mBuilder.length()); //使用之前首先清空內(nèi)容 for (int i = 0; i < DEFAULT_CODE_LENGTH; i++) { mBuilder.append(CHARS[mRandom.nextInt(CHARS.length)]); } return mBuilder.toString(); } //生成干擾線 private void drawLine(Canvas canvas, Paint paint) { int color = randomColor(); int startX = mRandom.nextInt(DEFAULT_WIDTH); int startY = mRandom.nextInt(DEFAULT_HEIGHT); int stopX = mRandom.nextInt(DEFAULT_WIDTH); int stopY = mRandom.nextInt(DEFAULT_HEIGHT); paint.setStrokeWidth(1); paint.setColor(color); canvas.drawLine(startX, startY, stopX, stopY, paint); } //隨機(jī)顏色 private int randomColor() { mBuilder.delete(0, mBuilder.length()); //使用之前首先清空內(nèi)容 String haxString; for (int i = 0; i < 3; i++) { haxString = Integer.toHexString(mRandom.nextInt(0xEE)); if (haxString.length() == 1) { haxString = "0" + haxString; } mBuilder.append(haxString); } return Color.parseColor("#" + mBuilder.toString()); } //隨機(jī)文本樣式 private void randomTextStyle(Paint paint) { int color = randomColor(); paint.setColor(color); paint.setFakeBoldText(mRandom.nextBoolean()); //true為粗體,false為非粗體 float skewX = mRandom.nextInt(11) / 10; skewX = mRandom.nextBoolean() ? skewX : -skewX; paint.setTextSkewX(skewX); //float類型參數(shù),負(fù)數(shù)表示右斜,整數(shù)左斜 paint.setUnderlineText(mRandom.nextBoolean()); //true為下劃線,false為非下劃線 paint.setStrikeThruText(mRandom.nextBoolean()); //true為刪除線,false為非刪除線 } //隨機(jī)間距 private void randomPadding() { mPaddingLeft += BASE_PADDING_LEFT + mRandom.nextInt(RANGE_PADDING_LEFT); mPaddingTop = BASE_PADDING_TOP + mRandom.nextInt(RANGE_PADDING_TOP); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
獲取Activity棧,判斷當(dāng)前Activity位置的方法
下面小編就為大家分享一篇獲取Activity棧,判斷當(dāng)前Activity位置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Android ActionBar制作時鐘實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了Android ActionBar制作時鐘的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android為按鈕控件綁定事件的五種實(shí)現(xiàn)方式
本篇文章主要是介紹了Android為按鈕控件綁定事件的五種實(shí)現(xiàn)方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11Android檢查手機(jī)有沒有安裝某應(yīng)用的方法
這篇文章主要介紹了Android檢查手機(jī)有沒有安裝某應(yīng)用的方法,分析總結(jié)了幾種常用的判斷技巧,涉及Android針對應(yīng)用程序包的相關(guān)讀取與判定技巧,需要的朋友可以參考下2016-08-08Android之禁止ViewPager滑動實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android之禁止ViewPager滑動實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Android動畫之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
Android中的翻轉(zhuǎn)動畫效果的實(shí)現(xiàn),Android中并沒有提供直接做3D翻轉(zhuǎn)的動畫,所以關(guān)于3D翻轉(zhuǎn)的動畫效果需要我們自己實(shí)現(xiàn),那么我們首先來分析一下Animation 和 Transformation,感興趣的朋友可以了解下啊2013-01-01Android代碼實(shí)現(xiàn)新年賀卡動畫示例詳解
這篇文章主要為大家介紹了Android代碼實(shí)現(xiàn)新年賀卡動畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01