1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.mrwu.myapplication.MainActivity" 7 android:orientation="vertical"> 8 9 10 <TextView 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="登录界面" 14 android:textSize="30dp" 15 android:background="#DBDBDB" 16 android:layout_gravity="center"></TextView> 17 18 <EditText 19 android:id="@+id/et1" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:hint="请输入用户名"/> 23 24 <EditText 25 android:id="@+id/et2" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:password="true" 29 android:hint="请输入密码"/> 30 <Button 31 android:id="@+id/bt1" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="登录" 35 android:layout_gravity="center" 36 /> 37 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 <TextView 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:text="头像" 10 android:layout_gravity="center"/> 11 <ImageView 12 android:id="@+id/Iv1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:src="@mipmap/ic_launcher" 16 android:layout_gravity="center"/> 17 <RelativeLayout 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content"> 20 <TextView 21 android:id="@+id/tv1" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="姓名:"/> 25 <EditText 26 android:layout_width="350dp" 27 android:layout_height="wrap_content" 28 android:layout_alignParentTop="true" 29 android:layout_alignParentEnd="true" /> 30 </RelativeLayout> 31 <RelativeLayout 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content"> 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="我的性别是:"/> 38 <RadioButton 39 android:layout_marginLeft="100dp" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="男"/> 43 <RadioButton 44 android:layout_marginLeft="200dp" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:text="女"/> 48 49 </RelativeLayout> 50 <TextView 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:text="我喜欢的专业:"/> 54 55 <CheckBox 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:text="JAVA"/> 59 <CheckBox 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" 62 android:text="Android"/> 63 <CheckBox 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" 66 android:text="英语"/> 67 <CheckBox 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" 70 android:text="高数"/> 71 <RelativeLayout 72 android:layout_width="wrap_content" 73 android:layout_height="wrap_content"> 74 <TextView 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" 77 android:text="ip:" 78 android:textSize="25dp"/> 79 <EditText 80 android:id="@+id/et3" 81 android:layout_marginLeft="25dp" 82 android:layout_width="350dp" 83 android:layout_height="wrap_content" /> 84 </RelativeLayout> 85 <TextView 86 android:id="@+id/tv2" 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" /> 89 90 </LinearLayout>
1 package com.example.mrwu.myapplication; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.text.TextUtils; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 public class MainActivity extends AppCompatActivity { 13 EditText et1; 14 EditText et2; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 et1=(EditText) findViewById(R.id.et1); 21 22 et2=(EditText) findViewById(R.id.et2); 23 24 Button bt1 =(Button)findViewById(R.id.bt1); 25 bt1.setOnClickListener(new View.OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 String str1=et1.getText().toString(); 29 String str2=et2.getText().toString(); 30 if(TextUtils.isEmpty(str1)&&TextUtils.isEmpty(str2)){ 31 Toast.makeText(MainActivity.this,"请输入用户名和密码",Toast.LENGTH_SHORT).show(); 32 33 }else if(TextUtils.isEmpty(str1)&&str2!=null){ 34 Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show(); 35 36 37 }else if(TextUtils.isEmpty(str2)&&str1!=null){ 38 Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show(); 39 40 }else{ 41 Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); 42 Intent myIntent = new Intent(MainActivity.this,SecondActivty.class); 43 startActivity(myIntent); 44 } 45 46 47 } 48 49 }); 50 } 51 }
1 public class SecondActivty extends AppCompatActivity { 2 ImageView Iv1; 3 EditText et3; 4 TextView tv2; 5 6 @Override 7 protected void onCreate(@Nullable Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.mylayout); 10 Iv1 = (ImageView) findViewById(R.id.Iv1); 11 et3 = (EditText)findViewById(R.id.et3); 12 tv2 = (TextView)findViewById(R.id.tv2); 13 Iv1.setOnTouchListener(new View.OnTouchListener() { 14 @Override 15 public boolean onTouch(View v, MotionEvent event) { 16 if (event.getAction() == MotionEvent.ACTION_UP){ 17 float x= event.getX(); 18 float y=event.getY(); 19 String info = "X=" + x + "Y=" + y; 20 Toast.makeText(SecondActivty.this, info, Toast.LENGTH_LONG).show(); 21 } 22 return true; 23 } 24 25 }); 26 et3.setOnKeyListener(new View.OnKeyListener() { 27 @Override 28 public boolean onKey(View v, int keyCode, KeyEvent event) { 29 switch (event.getAction()){ 30 case KeyEvent.ACTION_UP: 31 String ip = et3.getText().toString(); 32 tv2.setText(Subs(ip)); 33 case KeyEvent.ACTION_DOWN: 34 break; 35 } 36 37 return false; 38 } 39 String Subs(String total){ 40 String news = ""; 41 for (int i =0;i<=total.length()/3;i++ ){ 42 if(i*3+3<total.length()){ 43 news = news+total.substring(i*3,Math.min(i*3+3,total.length()))+"-"; 44 }else{ 45 news = news+total.substring(i*3,Math.min(i*3+3,total.length())); 46 } 47 } 48 return news; 49 } 50 51 52 53 }); 54 } 55 56 57 }
1.代码格式没有固定的格式风格
2.控件ID的命名不规范,没有意义
3.全部代码没有注释
4.UI控件成员变量没有统一加上控件缩写作为后缀,这样容易与普通成员变量混淆
5.类名参数名没有用android开发默认的命名规则