学习开源项目MyDiary第一天

首先这是学习开源项目的地址https://github.com/DaxiaK/MyDiary,不知道为什么这软件最重要的功能写日记在AVD还有红米K30u都不能运行。今天先实现的是点击主页面的设置按钮,弹出一个对话框给用户选择(并不是进入一个新页面)。在MyDiary中,点击对话框的图标都会跳转到新的页面(Activity),本次先将这些部分省略,改成点击每一个图标在LogCat中显示被点中的图标。这里需要注意的是drawable文件如图片的命名规则是只能数字和英文小写,不能大写。

学习开源项目MyDiary第一天   学习开源项目MyDiary第一天                学习开源项目MyDiary第一天 学习开源项目MyDiary第一天

 

MainActivity.java

 1 package com.example.ourdiary.main;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.ImageView;
 8 
 9 import com.example.ourdiary.R;
10 
11 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
12 
13     private ImageView IV_main_setting;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         IV_main_setting = findViewById(R.id.IV_main_setting);
20         IV_main_setting.setOnClickListener(this);
21     }
22 
23     @Override
24     public void onClick(View view) {
25         switch (view.getId()) {
26             case R.id.IV_main_setting:
27                 MainSettingDialogFragment mainSettingDialogFragment = new MainSettingDialogFragment();
28                 mainSettingDialogFragment.show(getSupportFragmentManager(),"mainSettingDialogFragment");
29                 break;
30 
31 
32         }
33     }
34 }

 

MainSettingDialogFragment.java  (不是Activity只是普通的java类)

 1 package com.example.ourdiary.main;
 2 
 3 import android.app.Dialog;
 4 import android.graphics.Color;
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 import android.widget.ImageView;
11 import android.widget.RelativeLayout;
12 import android.widget.Toast;
13 
14 import androidx.annotation.ColorRes;
15 import androidx.annotation.NonNull;
16 import androidx.annotation.Nullable;
17 import androidx.appcompat.app.AppCompatDialogFragment;
18 
19 import com.example.ourdiary.R;
20 
21 public class MainSettingDialogFragment extends AppCompatDialogFragment implements View.OnClickListener{
22 
23     private RelativeLayout RL_main_setting_dialog;
24     private ImageView IV_main_setting_add_topic,IV_main_setting_setting_page,
25             IV_main_setting_setting_security,IV_main_setting_backup,IV_main_setting_about;
26 
27     public Dialog onCreateDialog(Bundle savedInstanceState) {
28         Dialog dialog = super.onCreateDialog(savedInstanceState);
29         return dialog;
30     }
31 
32     @Nullable
33     @Override
34     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
35 
36         this.getDialog().setCanceledOnTouchOutside(true);
37         View rootView = inflater.inflate(R.layout.bottom_sheet_main_setting,container);
38 
39         RL_main_setting_dialog = rootView.findViewById(R.id.RL_main_setting_dialog);
40         RL_main_setting_dialog.setBackgroundColor(Color.BLUE);
41         IV_main_setting_add_topic = rootView.findViewById(R.id.IV_main_setting_add_topic);
42         IV_main_setting_add_topic.setOnClickListener(this);
43         IV_main_setting_setting_page = rootView.findViewById(R.id.IV_main_setting_setting_page);
44         IV_main_setting_setting_page.setOnClickListener(this);
45         IV_main_setting_setting_security = rootView.findViewById(R.id.IV_main_setting_setting_security);
46         IV_main_setting_setting_security.setOnClickListener(this);
47         IV_main_setting_backup = rootView.findViewById(R.id.IV_main_setting_backup);
48         IV_main_setting_backup.setOnClickListener(this);
49         IV_main_setting_about = rootView.findViewById(R.id.IV_main_setting_about);
50         IV_main_setting_about.setOnClickListener(this);
51 
52         return rootView;
53     }
54 
55     @Override
56     public void onClick(View view) {
57         switch (view.getId()) {
58             case R.id.IV_main_setting_add_topic:
59                 Log.d("MainSettingDFragment","成功点击第一个图标");
60                 break;
61             case R.id.IV_main_setting_setting_page:
62                 Log.d("MainSettingDFragment","成功点击第二个图标");
63                 break;
64             case R.id.IV_main_setting_setting_security:
65                 Log.d("MainSettingDFragment","成功点击第三个图标");
66                 break;
67             case R.id.IV_main_setting_backup:
68                 Log.d("MainSettingDFragment","成功点击第四个图标");
69                 break;
70             case R.id.IV_main_setting_about:
71                 Log.d("MainSettingDFragment","成功点击第五个图标");
72                 break;
73         }
74     }
75 }

 

activity.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <ImageView
 7         android:id="@+id/IV_main_setting"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_alignParentRight="true"
11         android:layout_alignParentBottom="true"
12         android:layout_centerVertical="true"
13         android:src="@drawable/iv_main_setting"/>
14 
15 </RelativeLayout>

 

bottom_sheet_main_setting.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/RL_main_setting_dialog"
 4     android:layout_width="match_parent"
 5     android:layout_height="70dp">
 6 
 7     <ScrollView
 8         android:layout_width="match_parent"
 9         android:layout_height="70dp">
10 
11         <LinearLayout
12             android:layout_width="match_parent"
13             android:layout_height="match_parent"
14             android:layout_gravity="center"
15             android:gravity="center"
16             android:orientation="horizontal">
17 
18             <ImageView
19                 android:id="@+id/IV_main_setting_add_topic"
20                 android:layout_width="wrap_content"
21                 android:layout_height="wrap_content"
22                 android:layout_gravity="center_vertical"
23                 android:src="@drawable/iv_main_setting_add_topic"/>
24 
25             <ImageView
26                 android:id="@+id/IV_main_setting_setting_page"
27                 android:layout_width="wrap_content"
28                 android:layout_height="wrap_content"
29                 android:layout_gravity="center_vertical"
30                 android:layout_marginLeft="20dp"
31                 android:src="@drawable/iv_main_setting_setting_page"/>
32 
33             <ImageView
34                 android:id="@+id/IV_main_setting_setting_security"
35                 android:layout_width="wrap_content"
36                 android:layout_height="wrap_content"
37                 android:layout_gravity="center_vertical"
38                 android:layout_marginLeft="20dp"
39                 android:src="@drawable/iv_main_setting_setting_security"/>
40 
41             <ImageView
42                 android:id="@+id/IV_main_setting_backup"
43                 android:layout_width="wrap_content"
44                 android:layout_height="wrap_content"
45                 android:layout_gravity="center_vertical"
46                 android:layout_marginLeft="20dp"
47                 android:src="@drawable/iv_main_setting_backup"/>
48 
49             <ImageView
50                 android:id="@+id/IV_main_setting_about"
51                 android:layout_width="wrap_content"
52                 android:layout_height="wrap_content"
53                 android:layout_gravity="center_vertical"
54                 android:layout_marginLeft="20dp"
55                 android:src="@drawable/iv_main_setting_about"/>
56 
57         </LinearLayout>
58 
59     </ScrollView>
60 
61 </RelativeLayout>

 

 最后在LogCat上看点击信息,设置LogCat的博客可以看https://blog.csdn.net/weixin_42950079/article/details/83544064

2021-03-16 23:23:37

上一篇:动画系列:java代码实现图片缩放动画


下一篇:LeetCode 1745. 回文串分割 IV(区间dp)