部分代码:
1. public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{ 2. private EditText etAccount, etPass, etPassConfirm; 3. private Button btnRegister, button2; 4. private CheckBox cbAgree; 5. private MySQLiteOpenHelper mySQLiteOpenHelper; 6. @Override 7. protected void onCreate(Bundle savedInstanceState) { 8. super.onCreate(savedInstanceState); 9. setContentView(R.layout.activity_register); 10. // 返回登录页面 11. button2 = (Button) findViewById(R.id.button2); 12. etAccount = findViewById(R.id.et_account1); 13. etPass = findViewById(R.id.et_password1); 14. etPassConfirm = findViewById(R.id.et_password_confirm); 15. btnRegister = findViewById(R.id.btn_register); 16. cbAgree = findViewById(R.id.cb_Agree); 17. button2.setOnClickListener(new View.OnClickListener() { 18. @Override 19. public void onClick(View view) { 20. //Intent intent = new Intent(MainActivity.this,DetailActivity.class); 21. Intent intent = new Intent(RegisterActivity.this,LoginActivity.class); 22. startActivity(intent); 23. } 24. }); 25. // 第二种方式写监听事件 26. btnRegister.setOnClickListener(this); 27. } 28. @Override 29. public void onClick(View view) { 30. String account = etAccount.getText().toString().trim(); 31. String pass = etPass.getText().toString().trim(); 32. String passConfirm = etPassConfirm.getText().toString(); 33. // TextUtils.isEmpty() 和 name.isEmpty() 的区别 34. // 第一个可以判断 NULL 和 “” 两种为空的情况,且均判断为空 35. // 第二个只能判断“”这一种情况,如果为NULL则会报空指针异常。 36. if(TextUtils.isEmpty(account)){ 37. Toast.makeText(RegisterActivity.this,"用户名不能为空", Toast.LENGTH_LONG).show(); 38. return; 39. } 40. mySQLiteOpenHelper = new MySQLiteOpenHelper(this); 41. // 查询这个账号是否已经被注册 42. List<User> userList = mySQLiteOpenHelper.selectByAccount(account); 43. if(!userList.isEmpty()){ //说明已经被注册过了 44. Toast.makeText(RegisterActivity.this,"该账号已经被注册!",Toast.LENGTH_LONG).show(); 45. return; 46. } 47. if(TextUtils.isEmpty(pass)){ 48. Toast.makeText(RegisterActivity.this,"密码不能为空",Toast.LENGTH_LONG).show(); 49. return; 50. } 51. if(!TextUtils.equals(pass,passConfirm)){ 52. Toast.makeText(RegisterActivity.this,"两次密码不一致",Toast.LENGTH_LONG).show(); 53. return; 54. } 55. if(!cbAgree.isChecked()){ 56. Toast.makeText(RegisterActivity.this,"请同意用户协议",Toast.LENGTH_LONG).show(); 57. return; 58. } 59. User user = new User(); 60. user.setAccount(account); 61. user.setPass(pass); 62. //插入数据到数据库中 63. mySQLiteOpenHelper = new MySQLiteOpenHelper(this); 64. long rowId = mySQLiteOpenHelper.insertData(user); 65. if(rowId != -1){ 66. ToastUtil.toastShort(this,"注册成功"); 67. Intent intent = new Intent(RegisterActivity.this,LoginActivity.class); 68. startActivity(intent); 69. }else { 70. ToastUtil.toastShort(this,"注册失败"); 71. } 72. } 73. }
3.1.3 页面浏览功能:用户注册并登录成功后,可以进入系统浏览推荐、要闻、思想、综合这四个新闻页面。用户可以根据个人兴趣进行选择。
部分代码:
1. public class FragmentMain extends AppCompatActivity { 2. ViewPager viewPager; 3. List<Fragment> viewList; 4. TextView tuijian, yaowen, sixiang, zonghe; 5. List<String> titlelist; 6. @Override 7. protected void onCreate(Bundle savedInstanceState) { 8. super.onCreate(savedInstanceState); 9. setContentView(R.layout.fragmentmian); 10. viewPager = findViewById(R.id.viewpager); 11. tuijian = findViewById(R.id.tuijian); 12. yaowen = findViewById(R.id.yaowen); 13. sixiang = findViewById(R.id.sixiang); 14. zonghe = findViewById(R.id.zonghe); 15. MyLister lister = new MyLister(); 16. tuijian.setOnClickListener(lister); 17. yaowen.setOnClickListener(lister); 18. sixiang.setOnClickListener(lister); 19. zonghe.setOnClickListener(lister); 20. viewList = new ArrayList<>(); 21. // View view1 = View.inflate(this, R.layout.fragment01, null); 22. // View view2 = View.inflate(this, R.layout.fragment02, null); 23. // View view3 = View.inflate(this, R.layout.fragment03, null); 24. // View view4 = View.inflate(this, R.layout.fragment04, null); 25. 26. titlelist=new ArrayList<>(); 27. titlelist.add("推荐"); 28. titlelist.add("要闻"); 29. titlelist.add("思想"); 30. titlelist.add("综合"); 31. // viewList.add(view1); 32. // viewList.add(view2); 33. // viewList.add(view3); 34. // viewList.add(view4); 35. viewList.add(new Fragment01()); 36. viewList.add(new Fragment02()); 37. viewList.add(new Fragment03()); 38. viewList.add(new Fragment04()); 39. //FragPageAdapter pageAdapter = new FragPageAdapter(viewList,titlelist); 40. FragmentManager manager=getSupportFragmentManager(); 41. viewPager.setAdapter(new FragfragAdapter(manager,viewList,titlelist)); 42. class MyLister implements View.OnClickListener { 43. @Override 44. public void onClick(View view) { 45. switch (view.getId()) { 46. case R.id.tuijian: 47. viewPager.setCurrentItem(0); 48. break; 49. case R.id.yaowen: 50. viewPager.setCurrentItem(1); 51. break; 52. case R.id.sixiang: 53. viewPager.setCurrentItem(2); 54. break; 55. case R.id.zonghe: 56. viewPager.setCurrentItem(3); 57. break; 58. } 59. } 60. } 61. }
3.1.4 新闻查看功能:用户登录成功后通过浏览推荐、要闻、思想、综合这四个新闻页面,如果发现非常感兴趣的新闻标题或图片,可以点击该新闻进入详情页面进行阅读。
部分代码:
1. public class Fragment01 extends Fragment { 2. private ListView Lv = null; 3. @Nullable 4. @Override 5. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 6. //解析碎片对应的布局文件 7. View view= inflater.inflate(R.layout.fragment01,container,false); 8. // return view; 9. Lv = view.findViewById(R.id.Lv); 10. final String[] name = new String[] { "******" }; 11. final String[] message = new String[] { 12. "******", 13. "******", 14. "******", 15. "******", 16. "******"}; 17. final int[] photo = new int[] { R.drawable.tuijian02, R.drawable.tuijian03, R.drawable.yaowen01 ,R.drawable.sixiang01, R.drawable.zonghe01}; 18. List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); 19. Map<String, Object> map1 = new HashMap<String, Object>(); 20. map1.put("photo", R.drawable.tuijian02); 21. map1.put("name", name[0]); 22. data.add(map1); 23. Map<String, Object> map2 = new HashMap<String, Object>(); 24. map2.put("photo", R.drawable.tuijian03); 25. map2.put("name", name[1]); 26. data.add(map2); 27. Map<String, Object> map3 = new HashMap<String, Object>(); 28. map3.put("photo", R.drawable.yaowen01); 29. map3.put("name", name[2]); 30. data.add(map3); 31. Map<String, Object> map4 = new HashMap<String, Object>(); 32. map4.put("photo", R.drawable.sixiang01); 33. map4.put("name", name[3]); 34. data.add(map4); 35. Map<String, Object> map5 = new HashMap<String, Object>(); 36. map5.put("photo", R.drawable.zonghe01); 37. map5.put("name", name[4]); 38. data.add(map5); 39. System.out.println(data); 40. Lv.setAdapter(new SimpleAdapter(getActivity(), 41. data, 42. R.layout.item, 43. new String[] { "photo", "name" }, 44. new int[] { R.id.iv,R.id.tv_name })); 45. Lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 46. @Override 47. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 48. Bundle bundle = new Bundle(); 49. bundle.putInt("photo", photo[arg2]); 50. bundle.putString("message", message[arg2]); 51. Intent intent = new Intent(); 52. intent.putExtras(bundle); 53. intent.setClass(getActivity(), NewsTuijian.class); 54. Log.i("message", message[arg2]); 55. getActivity().startActivity(intent); 56. // startActivity(intent); 57. } 58. }); 59. return view; 60. } 61. }
4 项目总结及心得
通过这次学习,我了解到Activity作为Android开发系统中的四大组件之一,它的功能非常强大,用于提供用户交互界面和对用户输入的处理,每个Activity都会占用一个窗口来展示它的界面。以及AppCompatActivity支持Fragment的管理和兼容,在处理多个Fragment时,可以方便地使用进行管理和切换,使得界面的组合更加灵活。
个人设计是最容易提升自身代码水平的,通过这次设计,我深刻的认识到学习新技术和工具是必须的,Android生态系统每年都在迅速发展和壮大,新技术和工具不断涌现,作为学生我们需要保持持续学习,跟上技术发展的步伐。另外好的代码设计和架构对于项目成功至关重要:良好的代码设计和架构可以提高代码质量,同时还有助于提高应用的性能和稳定性。
注:由于源码量过多,需要的朋友可在资源中下载,也可私信我拿取!