Android实现登录界面功能和实现详解
来源:互联网 发布:源码数据库放在哪里 编辑:程序博客网 时间:2024/06/04 20:57
两个编辑框
一个输入账号 ,一个输入密码
一个按钮
点击按钮登录
判断账号和密码不为空
设置账号必须以字母开头
验证密码最小为8位,最大16位
设置可显示隐藏密码
最终运行效果如下
public class MainActivity extends AppCompatActivity { private EditText et_name,et_pass; SharedPreferences share; CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name=(EditText)findViewById(R.id.et_name); et_pass=(EditText)findViewById(R.id.et_pass); cb=(CheckBox)findViewById(R.id.cb); cb.setOnCheckedChangeListener(new CheckClick()); share=getSharedPreferences("login",MODE_PRIVATE); String name=share.getString("name",""); String pass=share.getString("pass",""); if(!name.isEmpty() && !pass.isEmpty()){ et_name.setText(name); et_pass.setText(pass); } Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } public void login(View view){ String name=et_name.getText().toString(); String pass=et_pass.getText().toString(); Log.i("MainActivity",name.isEmpty()+""); if(name.isEmpty() || pass.isEmpty()){ Toast.makeText(MainActivity.this,"账号密码都不能为空",Toast.LENGTH_SHORT).show(); return; } String first=name.substring(0,1); if(!first.matches("[a-zA-Z]")){ Toast.makeText(MainActivity.this,"账号必须以字母开头",Toast.LENGTH_SHORT).show(); return; } if(pass.length()>=8 && pass.length()<=16){ SharedPreferences.Editor edit = share.edit(); edit.putString("name",name); edit.putString("pass",pass); edit.commit(); Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); }else { Toast.makeText(MainActivity.this,"密码必须为8-16位", Toast.LENGTH_LONG).show(); } } class CheckClick implements CompoundButton.OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.i("MainActivtiy",""+isChecked); if(isChecked){ //密文 et_pass.setTransformationMethod(PasswordTransformationMethod.getInstance()); cb.setText("隐藏密码"); }else { //明文 et_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); cb.setText("显示密码"); } } }}
第二个主体类
public class Main2Activity extends AppCompatActivity { private TabHost tabHost; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); tabHost=(TabHost)findViewById(R.id.tabhost); vp=(ViewPager)findViewById(R.id.vp); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tag1").setIndicator("标题1").setContent(R.id.vp)); tabHost.addTab(tabHost.newTabSpec("tag2").setIndicator("标题2").setContent(R.id.vp)); List<Fragment> list=new ArrayList<>(); Fragment1 fragment1=new Fragment1(); Fragment2 fragment2=new Fragment2(); list.add(fragment1); list.add(fragment2); Myadapter myadapter=new Myadapter(getSupportFragmentManager(),list); vp.setAdapter(myadapter); tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { if(tabId.equals("tag1")){ vp.setCurrentItem(0); }else { vp.setCurrentItem(1); } } }); //vp.setCurrentItem(0); tabHost.setCurrentTabByTag("tag1"); } @Override protected void onResume() { super.onResume(); vp.setCurrentItem(1); vp.setCurrentItem(0); }}
第一个页面
public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.fragment1, null); Button btn=(Button) inflate.findViewById(R.id.btnstart); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent("org.frxm.zhoukao3.MyReceiver"); getActivity().sendBroadcast(intent); } }); return inflate; }}
第二个页面
public class Fragment2 extends Fragment { MyDbOpenHelper helper; SQLiteDatabase db; ListView lv; Context context; SimpleCursorAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.fragment2, null); lv=(ListView)inflate.findViewById(R.id.lv); context=getActivity(); helper=new MyDbOpenHelper(context); db=helper.getWritableDatabase(); db.delete("info",null,null); for (int i=0;i<10;i++){ ContentValues cv=new ContentValues(); cv.put("name","我是"+i); db.insert("info",null,cv); } Toast.makeText(context,"插入成功",Toast.LENGTH_SHORT).show(); Cursor info = db.query("info", null, null, null, null, null, null); adapter=new SimpleCursorAdapter(context,R.layout.list_item,info, new String[]{"_id","name"},new int[]{R.id.id,R.id.name}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lv.setAdapter(adapter); registerForContextMenu(lv); return inflate; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0,1, Menu.NONE,"修改"); menu.add(0,2,Menu.NONE,"删除"); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo view=( AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); int position = view.position; //获取索引对应的行的内容 Cursor cursor=(Cursor) adapter.getItem(position); int _id = cursor.getInt(0); switch (item.getItemId()){ case 1://修改 ContentValues cv=new ContentValues(); cv.put("name","修改后的值"); int info1 = db.update("info", cv, "_id=?", new String[]{String.valueOf(_id)}); if(info1>0){ Toast.makeText(context,"修改成功", Toast.LENGTH_LONG).show(); } break; case 2://删除 int info2 = db.delete("info", "_id=?", new String[]{String.valueOf(_id)}); if(info2>0){ Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show(); } break; } Cursor info = db.query("info", null, null, null, null, null, null); adapter.changeCursor(info); return super.onContextItemSelected(item); }}
适配器
public class Myadapter extends FragmentStatePagerAdapter { List<Fragment> list; public Myadapter(FragmentManager fm, List<Fragment> list) { super(fm); this.list=list; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); }}
OpenHelper
public class MyDbOpenHelper extends SQLiteOpenHelper{ private final static String name="mydb.db"; private final static int version=1; public MyDbOpenHelper(Context context) { super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table info(_id integer primary key autoincrement,name varchar(10))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
接受者
public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder=new NotificationCompat.Builder(context); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("标题"); builder.setContentText("我就是发送的内容"); manager.notify(1,builder.build()); }}
主体布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="org.frxm.zhoukao3.MainActivity"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:digits="@string/name_match" android:hint="请输入账号" /> <EditText android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberPassword" android:hint="请输入密码" android:onClick="login" /> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏密码" android:checked="true" /> <Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:gravity="center" android:onClick="login" /></LinearLayout>
第二个主体布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main2" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="org.frxm.zhoukao3.Main2Activity"> <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"></TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </FrameLayout> </LinearLayout> </TabHost></RelativeLayout>
第一个页面布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btnstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播" /></LinearLayout>
第二个页面布局
list_item布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/></LinearLayout>
阅读全文
0 0
- Android实现登录界面功能和实现详解
- Android_登录界面功能和实现详解
- Android 实现登录界面和功能实例
- Android实现登录界面和功能实例
- Android实现登录界面和功能实例
- Android 实现登录界面和功能实例
- Android实现登录界面和功能实例
- Android实现登录界面和功能实例
- android 登录界面功能实现
- Android实现简易登录界面详解
- Android——SharedPreferences实现登录界面的记住密码和自动登录功能
- Android——SharedPreferences实现登录界面的记住密码和自动登录功能
- Android 实现登录界面 记住账号密码等功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录功能实现
- Android 客户端登录界面实现
- 光纤KVM主备双连接链路技术印证无缝切换和零延迟
- Mask R-CNN的完整MXNet复现
- c3p0 xml配置文件模版 [html] view plain copy <c3p0-config> <default-config> <property name=
- Linux中记录用户信息文件
- 拒绝干扰 解决Wi-Fi的最大问题
- Android实现登录界面功能和实现详解
- 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等)
- QGis二次开发基础 -- 构建图层管理器
- PHP开发APP接口(二)
- sonar 输出日志分析
- jQueryUI 日历
- leetcode 31. Next Permutation
- android popupwindow在屏幕内水平居中
- 7000个传感器遍布街道 控江路街道完成社区物联网工程第一期建设