Android实现登录界面功能和实现详解

两个编辑框

一个输入账号 ,一个输入密码

一个按钮

点击按钮登录

判断账号和密码不为空

设置账号必须以字母开头

验证密码最小为8位,最大16位

设置可显示隐藏密码



最终运行效果如下






[java]  view plain  copy
  1. public class MainActivity extends AppCompatActivity {  
  2.     private EditText et_name,et_pass;  
  3.     SharedPreferences share;  
  4.     CheckBox cb;  
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         et_name=(EditText)findViewById(R.id.et_name);  
  10.         et_pass=(EditText)findViewById(R.id.et_pass);  
  11.         cb=(CheckBox)findViewById(R.id.cb);  
  12.         cb.setOnCheckedChangeListener(new CheckClick());  
  13.   
  14.         share=getSharedPreferences("login",MODE_PRIVATE);  
  15.   
  16.         String name=share.getString("name","");  
  17.         String pass=share.getString("pass","");  
  18.         if(!name.isEmpty() && !pass.isEmpty()){  
  19.             et_name.setText(name);  
  20.             et_pass.setText(pass);  
  21.         }  
  22.         Intent intent=new Intent(MainActivity.this,Main2Activity.class);  
  23.         startActivity(intent);  
  24.     }  
  25.   
  26.     public void login(View view){  
  27.         String name=et_name.getText().toString();  
  28.         String pass=et_pass.getText().toString();  
  29.   
  30.         Log.i("MainActivity",name.isEmpty()+"");  
  31.         if(name.isEmpty() || pass.isEmpty()){  
  32.             Toast.makeText(MainActivity.this,"账号密码都不能为空",Toast.LENGTH_SHORT).show();  
  33.             return;  
  34.         }  
  35.         String first=name.substring(0,1);  
  36.         if(!first.matches("[a-zA-Z]")){  
  37.             Toast.makeText(MainActivity.this,"账号必须以字母开头",Toast.LENGTH_SHORT).show();  
  38.             return;  
  39.         }  
  40.   
  41.         if(pass.length()>=8 && pass.length()<=16){  
  42.             SharedPreferences.Editor edit = share.edit();  
  43.             edit.putString("name",name);  
  44.             edit.putString("pass",pass);  
  45.             edit.commit();  
  46.   
  47.             Intent intent=new Intent(MainActivity.this,Main2Activity.class);  
  48.             startActivity(intent);  
  49.         }else {  
  50.             Toast.makeText(MainActivity.this,"密码必须为8-16位", Toast.LENGTH_LONG).show();  
  51.         }  
  52.     }  
  53.   
  54.     class CheckClick implements CompoundButton.OnCheckedChangeListener{  
  55.   
  56.         @Override  
  57.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  58.             Log.i("MainActivtiy",""+isChecked);  
  59.             if(isChecked){  
  60.                 //密文  
  61.                 et_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());  
  62.                 cb.setText("隐藏密码");  
  63.             }else {  
  64.                 //明文  
  65.                 et_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());  
  66.                 cb.setText("显示密码");  
  67.             }  
  68.         }  
  69.     }  
  70. }  

第二个主体类

[java]  view plain  copy
  1. public class Main2Activity extends AppCompatActivity {  
  2.     private TabHost tabHost;  
  3.     private ViewPager vp;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main2);  
  8.         tabHost=(TabHost)findViewById(R.id.tabhost);  
  9.         vp=(ViewPager)findViewById(R.id.vp);  
  10.         tabHost.setup();  
  11.         tabHost.addTab(tabHost.newTabSpec("tag1").setIndicator("标题1").setContent(R.id.vp));  
  12.         tabHost.addTab(tabHost.newTabSpec("tag2").setIndicator("标题2").setContent(R.id.vp));  
  13.   
  14.         List<Fragment> list=new ArrayList<>();  
  15.         Fragment1 fragment1=new Fragment1();  
  16.         Fragment2 fragment2=new Fragment2();  
  17.         list.add(fragment1);  
  18.         list.add(fragment2);  
  19.         Myadapter myadapter=new Myadapter(getSupportFragmentManager(),list);  
  20.         vp.setAdapter(myadapter);  
  21.         tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {  
  22.             @Override  
  23.             public void onTabChanged(String tabId) {  
  24.                 if(tabId.equals("tag1")){  
  25.                     vp.setCurrentItem(0);  
  26.                 }else {  
  27.                     vp.setCurrentItem(1);  
  28.                 }  
  29.             }  
  30.         });  
  31.         //vp.setCurrentItem(0);  
  32.         tabHost.setCurrentTabByTag("tag1");  
  33.     }  
  34.   
  35.     @Override  
  36.     protected void onResume() {  
  37.         super.onResume();  
  38.         vp.setCurrentItem(1);  
  39.         vp.setCurrentItem(0);  
  40.     }  
  41. }  





[java]  view plain  copy
  1.   
第一个页面

[java]  view plain  copy
  1. public class Fragment1 extends Fragment {  
  2.     @Nullable  
  3.     @Override  
  4.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  5.         View inflate = inflater.inflate(R.layout.fragment1, null);  
  6.         Button btn=(Button) inflate.findViewById(R.id.btnstart);  
  7.         btn.setOnClickListener(new View.OnClickListener() {  
  8.             @Override  
  9.             public void onClick(View v) {  
  10.                 Intent intent=new Intent("org.frxm.zhoukao3.MyReceiver");  
  11.                 getActivity().sendBroadcast(intent);  
  12.             }  
  13.         });  
  14.         return inflate;  
  15.     }  
  16. }  

第二个页面

[java]  view plain  copy
  1. public class Fragment2 extends Fragment {  
  2.     MyDbOpenHelper helper;  
  3.     SQLiteDatabase db;  
  4.     ListView lv;  
  5.     Context context;  
  6.     SimpleCursorAdapter adapter;  
  7.     @Nullable  
  8.     @Override  
  9.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  10.         View inflate = inflater.inflate(R.layout.fragment2, null);  
  11.         lv=(ListView)inflate.findViewById(R.id.lv);  
  12.         context=getActivity();  
  13.         helper=new MyDbOpenHelper(context);  
  14.         db=helper.getWritableDatabase();  
  15.         db.delete("info",null,null);  
  16.   
  17.         for (int i=0;i<10;i++){  
  18.             ContentValues cv=new ContentValues();  
  19.             cv.put("name","我是"+i);  
  20.             db.insert("info",null,cv);  
  21.         }  
  22.         Toast.makeText(context,"插入成功",Toast.LENGTH_SHORT).show();  
  23.   
  24.         Cursor info = db.query("info"nullnullnullnullnullnull);  
  25.         adapter=new SimpleCursorAdapter(context,R.layout.list_item,info,  
  26.                 new String[]{"_id","name"},new int[]{R.id.id,R.id.name},  
  27.                 CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);  
  28.         lv.setAdapter(adapter);  
  29.         registerForContextMenu(lv);  
  30.         return inflate;  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {  
  35.         super.onCreateContextMenu(menu, v, menuInfo);  
  36.         menu.add(0,1, Menu.NONE,"修改");  
  37.         menu.add(0,2,Menu.NONE,"删除");  
  38.     }  
  39.   
  40.     @Override  
  41.     public boolean onContextItemSelected(MenuItem item) {  
  42.         AdapterView.AdapterContextMenuInfo view=( AdapterView.AdapterContextMenuInfo)item.getMenuInfo();  
  43.         int position = view.position;  
  44.         //获取索引对应的行的内容  
  45.         Cursor cursor=(Cursor) adapter.getItem(position);  
  46.         int _id = cursor.getInt(0);  
  47.         switch (item.getItemId()){  
  48.             case 1://修改  
  49.                 ContentValues cv=new ContentValues();  
  50.                 cv.put("name","修改后的值");  
  51.                 int info1 = db.update("info", cv, "_id=?"new String[]{String.valueOf(_id)});  
  52.                 if(info1>0){  
  53.                     Toast.makeText(context,"修改成功", Toast.LENGTH_LONG).show();  
  54.                 }  
  55.                 break;  
  56.             case 2://删除  
  57.                 int info2 = db.delete("info""_id=?"new String[]{String.valueOf(_id)});  
  58.                 if(info2>0){  
  59.                     Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();  
  60.                 }  
  61.                 break;  
  62.         }  
  63.         Cursor info = db.query("info"nullnullnullnullnullnull);  
  64.         adapter.changeCursor(info);  
  65.         return super.onContextItemSelected(item);  
  66.     }  
  67. }  

适配器
[java]  view plain  copy
  1. public class Myadapter extends FragmentStatePagerAdapter {  
  2.     List<Fragment> list;  
  3.     public Myadapter(FragmentManager fm, List<Fragment> list) {  
  4.         super(fm);  
  5.         this.list=list;  
  6.     }  
  7.   
  8.     @Override  
  9.     public Fragment getItem(int position) {  
  10.         return list.get(position);  
  11.     }  
  12.   
  13.     @Override  
  14.     public int getCount() {  
  15.         return list.size();  
  16.     }  
  17. }  

OpenHelper

[java]  view plain  copy
  1. public class MyDbOpenHelper extends SQLiteOpenHelper{  
  2.     private final static String name="mydb.db";  
  3.     private final static int version=1;  
  4.     public MyDbOpenHelper(Context context) {  
  5.         super(context, name, null, version);  
  6.     }  
  7.   
  8.     @Override  
  9.     public void onCreate(SQLiteDatabase db) {  
  10.         db.execSQL("create table info(_id integer primary key autoincrement,name varchar(10))");  
  11.     }  
  12.   
  13.     @Override  
  14.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  15.   
  16.     }  
  17. }  

接受者

[java]  view plain  copy
  1. public class MyReceiver extends BroadcastReceiver {  
  2.     public MyReceiver() {  
  3.     }  
  4.   
  5.     @Override  
  6.     public void onReceive(Context context, Intent intent) {  
  7.         NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
  8.         NotificationCompat.Builder builder=new NotificationCompat.Builder(context);  
  9.         builder.setSmallIcon(R.mipmap.ic_launcher);  
  10.         builder.setContentTitle("标题");  
  11.         builder.setContentText("我就是发送的内容");  
  12.         manager.notify(1,builder.build());  
  13.     }  
  14. }  

主体布局

[java]  view plain  copy
  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:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="org.frxm.zhoukao3.MainActivity">  
  9.   
  10.     <EditText  
  11.         android:id="@+id/et_name"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:inputType="text"  
  15.         android:digits="@string/name_match"  
  16.         android:hint="请输入账号" />  
  17.     <EditText  
  18.         android:id="@+id/et_pass"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:inputType="numberPassword"  
  22.         android:hint="请输入密码"  
  23.         android:onClick="login"  
  24.         />  
  25.     <CheckBox  
  26.         android:id="@+id/cb"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="隐藏密码"  
  30.         android:checked="true"  
  31.         />  
  32.     <Button  
  33.         android:id="@+id/login"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="登录"  
  37.         android:gravity="center"  
  38.         android:onClick="login"  
  39.         />  
  40. </LinearLayout>  

第二个主体布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main2"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="org.frxm.zhoukao3.Main2Activity">  
  8.     <TabHost  
  9.         android:id="@+id/tabhost"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent">  
  12.         <LinearLayout  
  13.             android:orientation="vertical"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="match_parent">  
  16.             <TabWidget  
  17.                 android:id="@android:id/tabs"  
  18.                 android:layout_width="match_parent"  
  19.                 android:layout_height="wrap_content"></TabWidget>  
  20.   
  21.             <FrameLayout  
  22.                 android:id="@android:id/tabcontent"  
  23.                 android:layout_width="match_parent"  
  24.                 android:layout_height="match_parent">  
  25.                 <android.support.v4.view.ViewPager  
  26.                     android:id="@+id/vp"  
  27.                     android:layout_width="match_parent"  
  28.                     android:layout_height="match_parent">  
  29.   
  30.                 </android.support.v4.view.ViewPager>  
  31.             </FrameLayout>  
  32.         </LinearLayout>  
  33.   
  34.     </TabHost>  
  35.   
  36. </RelativeLayout>  

第一个页面布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <Button  
  6.         android:id="@+id/btnstart"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="发送广播"  
  10.         />  
  11. </LinearLayout>  

第二个页面布局

[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <ListView  
  6.         android:id="@+id/lv"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         />  
  10. </LinearLayout>  


list_item布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <TextView  
  6.         android:id="@+id/id"  
  7.         android:layout_width="0dp"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_weight="1"/>  
  10.     <TextView  
  11.         android:id="@+id/name"  
  12.         android:layout_width="0dp"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_weight="1"/>  
  15. </LinearLayout>  

猜你喜欢

转载自blog.csdn.net/qq_39745566/article/details/80341765