關於製作習慣養成打卡仿uhabit的app

由於是剛接觸android studio 開發,就選擇了一個比較簡單的uhabit開源軟件,我寫的跟開源軟件裏面的代碼有很大的不同,但設計的功能,我是努力想做的跟uhabit相似的。但是最後還有好多沒有完成。

在寫的過程中遇到了很多問題

1.首先是對於一些文件的使用,比如style文件,是自己要添加東西進去然後供自己調用的,@style。。。之類的。

2.對於一些佈局,當時沒有好好看,在設計界面的時候吃虧了,比如LinearLayout是線性佈局,所以對於android:layout_above之類的擺放位置的就不能用了,還有一點不同的是,因爲在網上百度demo的時候,一直看到的是<RealitiveLayout,所以可以用

android :layout_above=",但是自己添加一個layout文件時系統自動定義的

<android.support.constraint.ConstraintLayout

這種情況下,就是用

app:layout_constraintTop_toBottomOf="@+id/edit_text1"形式。

3.對於設計一個好看的button,一直想把button變成沒有棱角的,還有自定義的圖片。

首先是設置一個imageButton

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jm.demo.register" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50dip"
        android:text="hello_world"
        android:paddingTop="20dp"
        android:paddingRight="20dp"
        android:paddingLeft="20dp"
        android:paddingBottom="20dp"
        android:background="@drawable/shape_world"/>

</RelativeLayout>

效果圖

 

其中的shape_world幫我實現了我想要的很多東西,比如背景,還有一個比較圓的button。

shape_worid是寫在drawable下面的一個文件

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape
            android:padding="10dp"
            android:shape="rectangle" >

            <!-- 實心 -->
            <solid android:color="#2e1f80" />
            <!-- 圓角 -->
            <corners android:radius="10dip" />
            <!-- <corners android:topRightRadius="@dimen/switch_button_inside_radius" /> -->
            <!-- <corners android:bottomRightRadius="@dimen/switch_button_inside_radius" /> -->
        </shape>
    </item>
    <item android:right="10dip">
        <shape android:shape="rectangle" ></shape>
    </item>

</layer-list>

至於爲什麼採用layer_list好像是因爲只有這樣才能把我想要的效果都實現。

4.在設計跳出來的對話框的時候。

 

主要是在一個按鈕裏面的onclick 實現的,

我做的是自定義的對話框,所以設計到了界面的設計。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/edit_text1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:textSize="15dp"
        android:text="習慣標題"
        android:textColor="#87d0eb"/>

    <EditText
        android:id="@+id/edit_edit1"
        android:paddingLeft="10dp"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:paddingTop="10dp"
        app:layout_constraintTop_toBottomOf="@+id/edit_text1" />

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        app:layout_constraintTop_toBottomOf="@+id/edit_text1"
        app:layout_constraintLeft_toRightOf="@id/edit_edit1"
        android:scaleType="fitXY"
        android:background="@drawable/edit_image_color"
        android:src="@drawable/tiaose"/>

    <TextView
        android:id="@+id/edit_text2"
        android:paddingLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#87d0eb"
        app:layout_constraintTop_toBottomOf="@id/edit_edit1"
        android:text="Question"/>

    <EditText
        android:id="@+id/edit_edit2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginRight="30dp"
        app:layout_constraintTop_toBottomOf="@+id/edit_text2"
        android:hint="eg:Did you exercise today?"/>

    <TextView
        android:id="@+id/edit_text3"
        app:layout_constraintTop_toBottomOf="@id/edit_edit2"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:paddingTop="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="10dp"
        android:text="重複" />
    <TextView
        android:id="@+id/edit_text4"
        app:layout_constraintTop_toBottomOf="@id/edit_text3"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginTop="50dp"
        android:paddingTop="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="10dp"
        android:text="提醒"/>
    <Spinner
        android:id="@+id/spin_chongfu"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/edit_text3"
        android:paddingTop="10dp"
        app:layout_constraintLeft_toRightOf="@id/edit_text3"
        android:paddingLeft="100dp"
        android:entries="@array/chongfu"/>
    <Spinner
        android:id="@+id/spin_tixing"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/edit_text4"
        android:paddingTop="10dp"
        app:layout_constraintLeft_toRightOf="@id/edit_text4"
        android:paddingLeft="100dp"
        android:entries="@array/tixing"/>


</android.support.constraint.ConstraintLayout>

這是界面

 case R.id.chuangjian:
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("添加習慣");
                //加載一個佈局
                View  view = LayoutInflater.from(MainActivity.this).inflate(R.layout.edit_habit_dialog,null);
                builder.setView(view);
                final EditText habit = (EditText)view.findViewById(R.id.edit_edit1);
                final EditText question = (EditText)view.findViewById(R.id.edit_edit2);
                builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                   public void onClick(DialogInterface dialog, int which) {                       
                        String a=habit.getText().toString();
                        String b=question.getText().toString();
                        Habit habitSql = new Habit(a,0);
                        habitreop.Habit_insert(habitSql);
                        HabitName h2 = new HabitName(a);
                        data.add(h2);
                        Toast.makeText(MainActivity.this,"用戶名:"+a+",密碼:"+b,Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("放棄", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {

                    }
                });
                builder.create().show();
        }

這是添加對話框的主要代碼,因爲對話框是自帶三個按鈕的,一個是放棄,一個是保存,一個是取消,直接用builder.***調用就好,不用再去添加button。

在這個設計界面裏面有個下拉框,spinner,在實現這個的時候,是在values裏面添加了一個arrays 文件,裏面定義的是下拉框的選項。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="languages">
        <item>c語言</item>
        <item>java </item>
        <item>php</item>
        <item>xml</item>
        <item>html</item>
    </string-array>
    <string-array name="chongfu">
        <item>每天</item>
        <item>每週</item>
        <item>每週兩次</item>
        <item>每週五次</item>
    </string-array>
    <string-array name="tixing">
        <item>1:20</item>
        <item>1:21 </item>
        <item>1:22</item>
        <item>1:23</item>
        <item>1:24</item>
    </string-array>


</resources>

還有一些關於listView和如何實現一個打卡簽到的功能,

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章