目前分類:Android-勞動部課程 (54)

瀏覽方式: 標題列表 簡短摘要

Screenshot_2019-04-25-11-31-24.png

 

●實作練習:xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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="match_parent"
    tools:context="com.example.student.lab0201.basketball">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="60dp"
            android:layout_gravity="center"/>
        <Button
            android:id="@+id/add3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+3 分球"
            android:textSize="20dp"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="8dp"
            android:onClick="add3"/>
        <Button
            android:id="@+id/add2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+2 分球"
            android:textSize="20dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:onClick="add2"/>
        <Button
            android:id="@+id/add1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="罰球"
            android:textSize="20dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:onClick="add1"/>

    </LinearLayout>
    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="重置"
        android:textSize="20dp"
        android:layout_marginBottom="16dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="clear"/>
</RelativeLayout>

 

●實作練習:java code

 

package com.example.student.lab0201;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class basketball extends AppCompatActivity {
    TextView score;
    Button add3, add2, add1, clear;
    private int mPoint=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basketball);
        score = (TextView) findViewById(R.id.score);
        add3 = (Button) findViewById(R.id.add3);
        add2 = (Button) findViewById(R.id.add2);
        add1 = (Button) findViewById(R.id.add1);
        clear = (Button) findViewById(R.id.clear);
    }

    //add 3 points
    public void add3(View v) {
        mPoint+=3;
        String mP=String.valueOf(mPoint);
        score.setText(mP);
    }

    //add 2 points
    public void add2(View v) {
        mPoint+=2;
        String mP=String.valueOf(mPoint);
        score.setText(mP);
    }

    //freethrow add 1 point
    public void add1(View v) {
        mPoint+=1;
        String mP=String.valueOf(mPoint);
        score.setText(mP);
    }

    //clean
    public void clear(View v) {
        //要先讓分數歸0
        mPoint=0;
        score.setText("0");
    }

}

 

*程式參考(gitHub):加減計算的練習與實作

文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

Screenshot_2019-04-25-11-30-47.png

●實作練習:xml

android:textAllCaps="true"  =>文字都大寫
<?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="match_parent"
    tools:context="com.example.student.lab0201.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp"
        tools:ignore="MissingConstraints">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Quantity"
            android:textAllCaps="true"
            android:textSize="25sp"
            android:paddingBottom="16dp"
            tools:ignore="HardcodedText" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+"
                android:textSize="25sp"
                android:layout_marginBottom="16dp"
                android:layout_marginRight="16dp"
                android:onClick="add"
                tools:ignore="HardcodedText,RtlHardcoded" />
            <TextView
                android:id="@+id/quantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="25sp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="16dp"
                tools:ignore="HardcodedText" />
            <Button
                android:id="@+id/subtract"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="-"
                android:textSize="25sp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:onClick="subtract"
                tools:ignore="HardcodedText,RtlHardcoded" />
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="price"
            android:textAllCaps="true"
            android:textSize="25sp"
            tools:ignore="HardcodedText" />
        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="25sp"
            android:textColor="@android:color/black"
            android:background="@android:color/holo_orange_light"/>
        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ok"
            android:textSize="25sp"
            android:onClick="send"
            tools:ignore="HardcodedText" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

 

 

●實作練習:java code

 

package com.example.student.lab0201;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;


public class MainActivity extends AppCompatActivity {
    TextView quantity;
    TextView price;
    //一般命名變數時,前面加個m代表整個類別可以使用的變數,設定初始值
    private int mQuantity=0;
    private int mPrice=5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        quantity=(TextView)findViewById(R.id.quantity);
        price=(TextView)findViewById(R.id.price);
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void send(View v){
        displaytotalPirce();

    }
    public void displayquantity(){
        String ntoString=String.valueOf(mQuantity);
        quantity.setText(ntoString);
    }
    public void add(View v){
        mQuantity++;
        displayquantity();
        price.setText("");
    }
    public void subtract(View v){
        if(mQuantity>0){
            mQuantity--;
            displayquantity();
        }
        price.setText("");

    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void displaytotalPirce(){
        int totalP=mQuantity*mPrice;
        //貨幣格式化=>可以出現$符號
        //AttentionYou should import "java.text.NumberFormat" here, or this app will crash.
        String pay=NumberFormat.getCurrencyInstance().format(totalP);
        String msg=pay+(mQuantity==0?"\nFree":"\nThank you!");
        price.setText(msg);

    }
}

*程式參考(gitHub):加減計算的練習與實作

文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●RelativeLayout實作範例(一):align練習(一)

1.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    android:padding="16dp">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/sasuke"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New message!"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:textSize="35sp"
        android:textColor="@android:color/white"
        android:background="#009668"/>

</RelativeLayout>

 

●RelativeLayout實作範例(二):align練習(二)

2.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="左上"
    android:textSize="34sp"
    android:background="@android:color/holo_red_light"
    android:textColor="@android:color/white"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右上"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左下"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右下"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左中"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下中"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上中"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中央"
        android:textSize="34sp"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/white"
        android:layout_centerInParent="true"
            />
</RelativeLayout>

 

●RelativeLayout實作範例(三):toRightof、toLeftof練習

3.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    >
    <TextView
        android:id="@+id/tom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_purple"
        android:layout_centerHorizontal="true"
        android:text="TOM"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/kevin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:background="@android:color/holo_purple"
        android:text="Kevin"
        android:layout_toLeftOf="@id/tom"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/john"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_purple"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/tom"
        android:layout_marginLeft="16dp"
        android:text="JOHN"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/amy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="AMY"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/Jimmy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:layout_marginBottom="16dp"
        android:layout_alignParentRight="true"
        android:layout_above="@id/amy"
        android:text="JIMMY"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/steve"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:layout_marginBottom="16dp"
        android:layout_alignParentRight="true"
        android:layout_above="@id/Jimmy"
        android:text="STEVE"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/linda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_dark"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="LINDA"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/maraia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_dark"
        android:layout_alignParentLeft="true"
        android:layout_above="@id/linda"
        android:text="MARIA"
        android:padding="16dp"
        android:layout_marginBottom="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/coco"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_dark"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/linda"
        android:text="COCO"
        android:padding="16dp"
        android:layout_marginLeft="16dp"
        android:textColor="@android:color/white"
        android:textSize="25sp"/>
</RelativeLayout>

 

●RelativeLayout實作範例(四):多層layout練習(ㄧ)--RelativeLayout包LinearLayout

4.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/kakashi"
            android:layout_alignParentTop="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旗木卡卡西"
            android:layout_gravity="center"
            android:textColor="@android:color/white"
            android:background="#009668"
            android:textSize="50sp"/>
    </LinearLayout>

</RelativeLayout>

 

●RelativeLayout實作範例(五):多層layout練習(二)--RelativeLayout包LinearLayout

5.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/holo_red_light"
        android:padding="16dp">
        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/sasuke"
            android:layout_marginRight="16dp"
            android:scaleType="centerCrop"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="90dp"
            android:text="佐助"
            android:gravity="center"
            android:background="#009668"
            android:paddingLeft="60dp"
            android:paddingRight="60dp"
            android:textSize="40sp"
            android:textColor="@android:color/white"/>
    </LinearLayout>
</RelativeLayout>

 

●RelativeLayout實作範例(六):多層layout練習(三)--RelativeLayout包RelativeLayout

6.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <RelativeLayout
       android:background="@android:color/holo_red_light"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:padding="16dp">
       <ImageView
           android:id="@+id/pic"
           android:layout_width="130dp"
           android:layout_height="130dp"
           android:src="@drawable/figure"/>
       <TextView
           android:id="@+id/text1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="火影忍者疾風傳"
           android:layout_toRightOf="@id/pic"
           android:layout_marginLeft="16dp"
           android:padding="15dp"
           android:textSize="25sp"
           android:textColor="@android:color/white"
           android:background="#009668"/>
       <TextView
           android:id="@+id/text2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="再一次通靈之術"
           android:layout_toRightOf="@id/pic"
           android:padding="15dp"
           android:layout_marginLeft="16dp"
           android:layout_marginTop="10dp"
           android:layout_below="@id/text1"
           android:textSize="25sp"
           android:textColor="@android:color/white"
           android:background="#009668"/>


   </RelativeLayout>
</RelativeLayout>

 

*程式參考(gitHub):layout實作練習

 

文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●關於layout的一些說明
1.新增layout
 
2.如果要使用新增的layout而非預設的,要到MainActivity.java去改設定
package com.example.student.lab01;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textview_layout);
    }
}
 
3.調整背景顏色時,可以按ctrl+\顯示提示
 
 
4.dp是給圖片大小用的,會讓圖片在任何裝置下大小都一樣,但文字要用sp,因為有些文字太小使用者可以在裝置上設定文字大小,如果用dp就會不管怎麼調整文字大小都一樣
 
●xml屬性介紹
1.gravity:設定元件中文字對齊的方式
2.layout_gravity:設定元件對齊父容器的方式
3.RelativeLayou沒有weight
4.maxlines:最多顯示的行數,可以輸入多行,maxlines=1一次只顯示一行
 
5.水平置中:android:layout_centerHorizontal="true"
6.垂直置中:android:layout_centerVertical="true"
7.在父容器正中央:android:layout_centerInParent="true"
 
●實作範例(一):gravity練習
擷取.PNG
 
 
程式碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="To"
        android:textSize="30sp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Subject"
        android:textSize="30sp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:hint="Message"
        android:maxLines="1"
        android:gravity="top"
        android:textSize="30sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textSize="30sp"
        android:text="SEND"/>

</LinearLayout>

 

●實作範例(二):weight練習

2.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="hello"
        android:textSize="36sp"
        android:background="@android:color/holo_blue_bright"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="java"
        android:textSize="36sp"
        android:background="@android:color/holo_red_light"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="android"
        android:textSize="36sp"
        android:background="@android:color/holo_green_light"
        android:layout_weight="1"
        />
</LinearLayout>

 

●實作範例(三):ImageView and ScaleType練習

3.PNG

 

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/naruto_sd"
        android:scaleType="centerInside"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Naruto SD"
        android:textColor="@android:color/white"
        android:textSize="54dp"
        android:gravity="center"
        android:background="#02a69b"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Power Shippuden"
        android:textColor="@android:color/white"
        android:textSize="36dp"
        android:gravity="center"
        android:background="#02a69b"/>
</LinearLayout>

 

*程式參考(gitHub):layout實作練習

文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●spinner=>選單清單
1.搭配陣列=>String[] 陣列物件{,,,};
2.ArrayAdapter<泛型> 物件=new ArrayAdapter<泛型>(this,android.R.layout.simple_spinner_item,陣列物件);
(=>收集資料/集合,很像ArrayList)
=>把陣列先放進來
=>只要元件名稱能變成陣列,就能用ArrayAdapter,可以支援很多元件,不限定於spinner
3.物件.setDropdownViewResourc(android.R.layout .simple_spinner .dropdown_item);
=>把陣列內容放進spinner中,這邊的物件是spinner物件(用dropdown的方式顯示)
4.spinner物件.setAdapter(adp物件);=>啟動spinner物件
 
●實作:設計一個下拉選單
public class MainActivity extends AppCompatActivity {
    Spinner spn;
    String [] x={"教育","金融","軍警"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         spn=(Spinner)findViewById(R.id.spn);
        ArrayAdapter<String> ada=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,x);
        ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn.setAdapter(ada);
    }
}
**說明:
1.先抓id,設定陣列內容
2.陣列的類型要看清單內容是甚麼類型,因為大部分下拉選單都是用文字,所以這邊使用String
3.new要在onCreate做,一啟動就載入陣列資料
4.ArrayAdapter<String> =>這邊<>中的類型,要和陣列類型一樣,因為泛型,要收集此類型的資料
5. ArrayAdapter<String> ada=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,x);=>把陣列資料放進ada這個adpter物件,且要使用spinner物件
6. ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);=>指定這個物件要用spinner_dropdown元件
7. spn.setAdapter(ada);=>把ada顯示在ui的元件上
 
public class MainActivity extends AppCompatActivity {
Spinner spn;
String [] x={"教育","金融", "服務"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spn=(Spinner)findViewById(R.id.spn);
ArrayAdapter<String> ada=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,x);
ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn.setAdapter(ada);
spn.setOnItemSelectedListener(L1);
}
Spinner.OnItemSelectedListener L1=new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adp, View view, int i, long l) {
String msg=adp.getSelectedItem().toString();
Toast t=Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT);
t.show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
 
}
};
}
**說明:
1.這邊要用 OnItemSelectedListener,一樣是匿名類別的作法,前面可加private也可不加
2.這個方法要override兩種, onItemSelected onNothingSelected,有選選項和沒選選項
3. onItemSelected(AdapterView<?> adp, View view, int i, long l)=>()中的 AdapterView<?> adp,因為內容是由Adapter產生給Spinner所以這邊是在等一個 AdapterView的物件進來,adp是一個可以自行決定的物件名稱,代表由上面ada把內容指定給它
4. adp.getSelectedItem().toString(); =>.getSelectedItem()是代表adp抓到的資料,因為是去R檔找的資料,所以會是int,這邊要把它轉成String
5.這邊只是把選單選取後要做甚麼功能完成,但當點下去要執行選單這個動作還沒寫,所以要再加上 spn.setOnItemSelectedListener(L1);
 
**也可在values/string.xml下做陣列,這樣不同業面都能共用這個下拉選單,用下列作法:
string.xml
<resources>
    <string name="app_name">Ex1</string>
    <string-array name="x2">
        <item>教育</item>
        <item>金融</item>
        <item>服務</item>
    </string-array>
</resources>
 
mainactivity.java
//ArrayAdapter<String> ada=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,x);
改成ArrayAdapter<CharSequence> ada=
ArrayAdapter.createFromResource(this,R.array.x2,android.R.layout.simple_spinner_item);
 
**說明:
因為這邊的 .createFromResource()是方法,不是建構式,而且是static,所以不需要new
 
 
*程式參考(gitHub):impelment of spinner
 
 
 
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

 
1)說明:
res:操作畫面(UI)有關的資源
drawable:圖片=>JPG,PNG,GIF...
layout:XML=>操作畫面
mipmap:ICON=>APP LOGO
values:每頁所需的標題,專有名稱
a.做兩個可以用按鈕切換的頁面,用values設定文字範本=>values/strings.xml,設定每個要使用文字的元件的代號
<resources>
    <string name="app_name">Test20</string>
    <string name="title1">比賽項目</string>
    <string name="t1_1">足球</string>
    <string name="t1_2">棒球</string>
    <string name="title2">身分別</string>
    <string name="t2_1">男</string>
    <string name="t2_2">女</string>
</resources>
 
**說明:
1.name="代號"
2. <string name="app_name">Test20</string>=>這邊兩個tag中間就是要顯示的內容
3.如果button的文字也想要設定,也可在此設定
 
 
b.切回ui介面使用@string/title1就可以抓到 strings.xml中設定的內容了(如下)
<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:text="@string/title1/>
 
c.不用onclick用別的做法切換頁面
private Button.OnClickListener k=new Button.OnClickListener(){
    @Override
    public void onClick(View view) {
        Intent in=new Intent();
        in.setClass(MainActivity.this,P2.class);
        startActivity(in);
    }
};
**說明:
1.因為這個功能只給 MainActivity這個class用,所以可以設定private
2. Button.打完之後精靈抓出來的全部都是按鈕操作的模式,選擇要用的
3. OnClickListener是interface,不能直接new,所以是用匿名類別的作法,實際上是new {}中的內容
4. OnClickListener是在等使用者按下去的動作
5.此時k就成為一個listener物件,裡面有onClick method,這個method就是要做煥頁的動作
 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    a1=(Button)findViewById(R.id.a1);
    a2=(Button)findViewById(R.id.a2);
    c1=(CheckBox) findViewById(R.id.c1);
    c2=(CheckBox) findViewById(R.id.c2);
    a1.setOnClickListener(k);
}
**說明:
1.按a1時,啟動k物件的內容,要寫在onCreate中,當app啟動時就要執行a1的等待功能
2.a1.setOnclickListener(k)就是在a1設定一個等user按下的功能
 
 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   
    a1=(Button)findViewById(R.id.a1);
    a2=(Button)findViewById(R.id.a2);
    c1=(CheckBox) findViewById(R.id.c1);
    c2=(CheckBox) findViewById(R.id.c2);
    a1.setOnClickListener(k);
    a2.setOnClickListener(k2);
 
}
private Button.OnClickListener k=new Button.OnClickListener(){
    @Override
    public void onClick(View view) {
        Intent in=new Intent();
        in.setClass(MainActivity.this,P2.class);
        startActivity(in);
    }
};
private Button.OnClickListener k2=new Button.OnClickListener(){
    @Override
    public void onClick(View view) {
String msg="";
if(c1.isChecked())msg=msg+"足球\n";
if(c2.isChecked())msg=msg+"棒球";
        Toast t=Toast.(MainActivity.this,"welcome to this world",Toast.LENGTH_LONG);
         t.setGravity(Gravity.CENTER,0,0);
         t.show();
}
};
**說明:
可以在下方寫很多功能,直接在上面的按鈕套用想要的功能
 
 
public class P2 extends AppCompatActivity {
    Button b1,b2;
    RadioButton r1,r2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_p2);
        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);
        r1=(RadioButton) findViewById(R.id.r1);
        r2=(RadioButton) findViewById(R.id.r2);
        b1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent in=new Intent();
                        in.setClass(P2.this,MainActivity.class);
                        startActivity(in);
                    }
                }
        );
    }
}
**說明:
在第二頁改為,不再new物件給k,直接把listener物件在setOnClickListener中new
 
 
2)顯示訊息=>Tosast(可以搭配if或exception,指定在某些狀況下出現)
a.Toast 物件名稱=Toast.makeText(目前的class.this,"內容",持續時間);
   物件名.show();
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast t=Toast.makeText(MainActivity.this,"welcome to this world",Toast.LENGTH_LONG);
        t.show();
    }
}
 
b.Toast.makeText(目前的class.this,"內容",時間).show();
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      Toast.makeText(MainActivity.this,"welcome to this world",Toast.LENGTH_LONG).show();
    }
}
 
c.如果直接用show()會放在最下面,要調整位置可以在show()之前用
=>setGravity(Gravity.位置,x,y)
=>位置:CENTER,LEFT,RIGHT....
=> x從上述指定位置往左(負)右(正)調整
=>y從上述指定位置往上(負)下(正)調整
 
*程式參考(gitHub):values/strings.xml and Toast
 
 
 
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●V(View)=>ImageView
1.一定要英文名字
2.要事先存檔,支援多種檔案格式ex:png.gif.jpg...
3.把檔案copy然後可以直接在專案的drawable資料夾上按右鍵選貼上,確定圖片的放置位置後,就會發現檔案已經copy好了
實作(一)用兩個按鈕切換圖片
1.版面做兩個button,然後加入一個imageView,選定要出現的第一張照片
 
2.設定id(pre/next/img)
**onclick=>android:onclick="",可以直接自己輸入
 
3.抓id
**View是所有操作元件的父類別, (View v)是代表在等一個View物件進來,以此為例,就是在按下按鈕的時候會new一個Button出來,然後就會把它抓過去
public class MainActivity extends AppCompatActivity {
    Button pre,next;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pre=(Button)findViewById(R.id.pre);
        next=(Button)findViewById(R.id.next);
        img=(ImageView)findViewById(R.id.img);               
    }
    public void pre(View v){
 
}
public void next(View v){
}
 
4.用setImageResource()函式把照片放進來ex:img.setImageResource(R.drawable.img01);
public class MainActivity extends AppCompatActivity {
    Button pre,next;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pre=(Button)findViewById(R.id.pre);
        next=(Button)findViewById(R.id.next);
        img=(ImageView)findViewById(R.id.img);
 
    }
    int[] s={
    R.drawable.img01,
    R.drawable.img02,
    R.drawable.img03,
    R.drawable.img04,
    R.drawable.img05};
  
     int i=-1;
     public void pre(View v){
    i--;
    if(i<0){i=s.length-1;}
    img.setImageResource(s[i]);
 
}
public void next(View v){
        i++;
        if(i== s.length ){i=0;}
        img.setImageResource(s[i]);
 
}
}
**說明:
1. 用陣列設計可以點一按鈕就換到下一張圖的效果,兩個方法的變數要用同一個全域變數,因為照片是共用的
2.當i==s.length時就要從第一張照片開始,所以指定i=0
3.當i<0時,就要從最後一張照片開始,所以指定i=s.length-1
 
5.安裝桌面圖示,把要用的圖片放在mipmap資料夾下,再去修改manifests下的AndroidManifest.xml檔的
android:icon="@mipmap/logo",斜線後面的名稱即可
若是使用模擬器,要改
android:roundIcon="@mipmap/logo"
 
*程式參考(github):Change Image When Clicking Button
 
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

(一)商品和加購金額的計算頁面
1.可以輸入尺和筆的數量,還有勾選紙/圓規/筆記本,計算總共金額(尺:29元,筆31元,紙加購3份60元,圓規3支60元,筆記本3本60元),先做版面設計
2.設定id和定義變數
public class MainActivity extends AppCompatActivity {
    EditText x,y;
    CheckBox s1,s2,s3;
    Button cal;
    TextView ans;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x=(EditText)findViewById(R.id.x);
        y=(EditText)findViewById(R.id.y);
        s1=(CheckBox)findViewById(R.id.s1);
        s2=(CheckBox)findViewById(R.id.s2);
        s3=(CheckBox)findViewById(R.id.s3);
        cal=(Button)findViewById(R.id.cal);
        ans=(TextView)findViewById(R.id.ans);
    }
    public void cal(View v){
       
    }
}
 
3.加入功能
public void cal(View v){
try{
int X=Integer.parseInt(x.getText().toString());
int Y=Integer.parseInt(y.getText().toString());
boolean S1=s1.isChecked();
boolean S2=s2.isChecked();
boolean S3=s3.isChecked();
int sum=29*X+31*Y;
if (S1==true){
  sum=sum+30;
}
if (S2==true){
    sum=sum+60;
}
if (S3==true){
    sum=sum+60;
}
ans.setText("總金額合計="+sum+"元");
}catch (Exception e){
    ans.setText("不可輸入空白");
}
}
**說明:
1.isChecked():boolean確定checkbox是某有被勾選,有勾選為true,沒勾選為false
2.三個為獨立的if互不影響,因為可以複選
3.輸入空白送出會出錯,因為有Exception,所以要用try...catch,雖然不知道Exeception內容,可以直接用最上層的Exception抓
 
 
(二)
1.做一個按鈕可以從主頁跳到下一頁(P2),並且切換回來
 
MainActivity.java
 
public class MainActivity extends AppCompatActivity {
    EditText x,y;
    CheckBox s1,s2,s3;
    Button cal,btnP2;
    TextView ans;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
...
        btnP2=(Button)findViewById(R.id.btnP2);
    }
    public void btnP2(View v){
        Intent in=new Intent();
        in.setClass(MainActivity.this,P2.class);
        startActivity(in);
    }
   ...
}
 
P2.java
public class P2 extends AppCompatActivity {
    Button home;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_p2);
        home=(Button)findViewById(R.id.home);
    }
    public void home(View v){
        Intent in=new Intent();
        in.setClass(P2.this,MainActivity.class);
        startActivity(in);
    }
}
 
2.設計版面,定ID,定義變數(在做radio button時,如果要單選,要先放radio group讓他們變成一個群組)
 
public class P2 extends AppCompatActivity {
    Button home,ok;
    RadioButton b1,b2,b3;
    CheckBox c1,c2,c3;
    TextView ans2;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_p2);
        home=(Button)findViewById(R.id.home);
        ok=(Button)findViewById(R.id.ok);
        b1=(RadioButton)findViewById(R.id.b1);
        b2=(RadioButton)findViewById(R.id.b2);
        b3=(RadioButton)findViewById(R.id.b3);
        c1=(CheckBox)findViewById(R.id.c1);
        c2=(CheckBox)findViewById(R.id.c2);
        c3=(CheckBox)findViewById(R.id.c3);
        ans2=(TextView)findViewById(R.id.ans2);
    }
    public void home(View v){
        Intent in=new Intent();
        in.setClass(P2.this,MainActivity.class);
        startActivity(in);
    }
 
}
 
3.撰寫程式碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_p2);
home=(Button)findViewById(R.id.home);
ok=(Button)findViewById(R.id.ok);
b1=(RadioButton)findViewById(R.id.b1);
b2=(RadioButton)findViewById(R.id.b2);
b3=(RadioButton)findViewById(R.id.b3);
c1=(CheckBox)findViewById(R.id.c1);
c2=(CheckBox)findViewById(R.id.c2);
c3=(CheckBox)findViewById(R.id.c3);
ans2=(TextView)findViewById(R.id.ans2);
}
public void home(View v){
Intent in=new Intent();
in.setClass(P2.this,MainActivity.class);
startActivity(in);
}
public void ok(View v){
String job="";
String interest="";
if (b1.isChecked()){
job="資訊";
}else if(b2.isChecked()){
job="教育";
}else if(b3.isChecked()){
job="金融";
}
 
//if(!(c1.isChecked())&&!(c2.isChecked())&&!(c3.isChecked()))interest=interest+"請選擇任一興趣!";
if(c1.isChecked())interest=interest+" 電影";
if(c2.isChecked())interest=interest+" 運動";
if(c3.isChecked())interest=interest+" 音樂";
ans2.setText("您的職業是:"+job+"\n您的興趣是:"+interest);
}
}
 
(三)練習把網站做成APP(參考:http://163.20.103.193/~chen06/b/standard.htm)
 
*程式參考(github):checkbox & radiobutton
*程式參考(github):網站改寫為App

 

文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●android的功能如果要運算或是有要寫必較複雜的函式,可以把class用java寫好在import進來,因為打開很慢測試也不方便
=>m(model):開發jar=>jar/doc
=>v(view):ui(user interface)=>app
=>c(controller)=>使用android=>api=>軟體/硬體=>結合雲端firebase=>import 設計jar(要給android用的盡量設計成內部class且都用static)
 
●寫內部類別時如果忘記怎麼寫可以參考android的R檔寫法,找R檔可以先把android檔案管理模式切換成package,R檔都是註冊元件,不會有java檔
 
●匯入jar檔使用(一定要寫說明文件)
1.先把要匯入的jar檔放到專案下的app/libs下
2.到android檔案管理區,在該jar檔上按右鍵,選add library as...就可以匯入了
 
**如果發現匯入至android時出現*,代表沒有抓到class
 
**因為android目前支援的jar是7.0,但我們做的jar是8.0所以匯入後執行時會有問題
**jar降版本要在專案的properpites中的source/Binary Format改
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

MainActitvity.java
public class MainActivity extends AppCompatActivity {
    Button m1,m2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m1=(Button) findViewById(R.id.m1);
        m2=(Button) findViewById(R.id.m2);
    }
    public void ex3(View v){
        Intent in=new Intent();
        in.setClass(MainActivity.this,Ex3.class);
        startActivity(in);
    }
 
Ex3.java
public class Ex3 extends AppCompatActivity {
    Button home2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex3);
        home2=(Button)findViewById(R.id.home2);
    }
    public void home2(View v){
        Intent in=new Intent();
        in.setClass(Ex3.this,MainActivity.class);
        startActivity(in);
    }
}
 
●再新增一頁Ex3_1,可以回上一頁(Ex3),Ex3新增一按鈕可以跳到Ex3_1
Ex3.java
public class Ex3 extends AppCompatActivity {
    Button home2,ex3_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex3);
        home2=(Button)findViewById(R.id.home2);
        ex3_1=(Button)findViewById(R.id.ex3_1);
    }
    public void ex3_1(View v){
        Intent in=new Intent();
        in.setClass(Ex3.this,Ex3_1.class);
        startActivity(in);
    }
Ex3_1.java
public class Ex3_1 extends AppCompatActivity {
Button ex3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex3_1);
ex3=(Button)findViewById(R.id.ex3);
}
public void ex3(View v){
Intent in=new Intent();
in.setClass(Ex3_1.this,Ex3.class);
startActivity(in);
}
}
(一)設計版面
 
(二)命名id,本金(pv)、 利率(r) 、 年期(n) 、 計算(cal)、 顯示(fv),定義變數和抓id,注意這邊變數的類別有不同
public class Ex3_1 extends AppCompatActivity {
    Button ex3,cal;
    EditText pv,r,n;
    TextView fv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex3_1);
        ex3=(Button)findViewById(R.id.ex3);
        cal=(Button)findViewById(R.id.cal);
        pv=(EditText)findViewById(R.id.pv);
        r=(EditText)findViewById(R.id.r);
        n=(EditText)findViewById(R.id.n);
        fv=(TextView)findViewById(R.id.fv);
    }
...
}
 
(三)寫函式
public class Ex3_1 extends AppCompatActivity {
   ...(前面省略)
    public void cal(View v){
        int PV=Integer.parseInt(pv.getText().toString());
        double R =Double.parseDouble(r.getText().toString()) ;
        int N=Integer.parseInt(n.getText().toString());
        fv.setText("複利本利和="+PV*Math.pow((1+R),N));
    }
}
**說明:
1.在android抓輸入資料和輸出資料一樣用getText()和setText()
2.但pv.getText()抓回來不是String而是Edittext類型,如下圖,要用toString()把Edittext轉成字串( toString()可以把所有非字串的物件轉成字串)
3.但因為還要把輸入的資料拿來計算,所以要再轉成int和double,做法和java完全一樣
 
*衍生練習(github黨):躉繳保費的內部報酬率計算
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●(一)新增一個按鈕,點擊可以直接連到goolge網址
**(Action_view,uri)
1)瀏覽器=>Http://
2)電話=>tel:
搭配uri物件
 
1.介面設定button(大小字型等),指定id(btnGoogle)和函式名稱(btnGoogle)
2.定義變數,抓id,寫功能
public class Ex2 extends AppCompatActivity {
    Button home1,btnGoogle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex2);
        home1=(Button)findViewById(R.id.home1);
        btnGoogle=(Button)findViewById(R.id.btnGoogle);
    }
    public void home(View v){
        Intent in=new Intent();
        in.setClass(Ex2.this,MainActivity.class);
        startActivity(in);
    }
    public void btnGoogle(View v){
        Uri google=Uri.parse("http://www.google.com.tw");
        Intent in=new Intent(Intent.ACTION_VIEW,google);
        startActivity(in);
    }
}
**說明:
1.要先產生uri物件, Uri google=Uri.parse("http://www.google.com.tw");=>透過google(可隨意定)轉成Uri物件,因為Uri是static所以不用new,parse()=>是把字串轉成uri物件類型的方法
2.我們要用的是 Intent(Intent.ACTION_VIEW,google);=>這是Intent的建構式, ACTION_VIEW,前面的 ACTION是動作, _VIEW,是要做的功能
 
(二)增加一個撥電話的按鈕
public class Ex2 extends AppCompatActivity {
    Button home1,btnGoogle,btnTel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex2);
        home1=(Button)findViewById(R.id.home1);
        btnGoogle=(Button)findViewById(R.id.btnGoogle);
        btnTel=(Button)findViewById(R.id.btnTel);
    }
    public void btnTel(View v){
        Uri tel=Uri.parse("tel:21345678");
        Intent in = new Intent(Intent.ACTION_DIAL,tel);
        startActivity(in);
    }
 .....(下面程式與上面同先省略)
}
**說明:
1.先定義變數,抓id
2.這邊撥電話在parse中要輸入("tel:....")=>大小寫都可以
3.action要選擇ACTION_DAIL
 
(三)增加一個啟動相機的按鈕
public class Ex2 extends AppCompatActivity {
Button home1,btnGoogle,btnTel,btnCam;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex2);
home1=(Button)findViewById(R.id.home1);
btnGoogle=(Button)findViewById(R.id.btnGoogle);
btnTel=(Button)findViewById(R.id.btnTel);
btnCam=(Button)findViewById(R.id.btnCam);
}
public void btnCam (View v){
Intent in=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(in);
}
...
}
**說明:
1.開啟相機要用 MediaStore.ACTION_IMAGE_CAPTURE
2.這次用到的建構式為Intent(String action),只放一個action當引數
 
(四)增加一個啟動設定好位置的google map
public class Ex2 extends AppCompatActivity {
    Button home1,btnGoogle,btnTel,btnCam,btnMap;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex2);
        home1=(Button)findViewById(R.id.home1);
        btnGoogle=(Button)findViewById(R.id.btnGoogle);
        btnTel=(Button)findViewById(R.id.btnTel);
        btnCam=(Button)findViewById(R.id.btnCam);
        btnMap=(Button)findViewById(R.id.btnMap);
    }
    public void btnMap(View v){
        Uri map=Uri.parse("geo:25.096275, 121.516642");
        Intent in=new Intent(Intent.ACTION_VIEW,map);
        startActivity(in);
    }
**說明:
一樣使用Uri.parse(),但裡面放geo:緯度,經度,同樣使用ACTION_VIEW
 
*衍生練習(github):ACTION_VIEW的實作練習
 
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

 
●實作:做兩個頁面切換,第一頁是MainActivity,有一個按鈕(id=m1),可以切換到第二頁有主題內容+一個button(id=home1)可以切回首頁
 
1.先設定首頁的按鈕ID(m1)和onClick函式名稱(ex2),並調整按鈕文字內容和大小
 
2.宣告變數,抓id(記得轉型),先建立onClick函式外面
public class MainActivity extends AppCompatActivity {
    Button m1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m1=(Button) findViewById(R.id.m1);
    }
     public void ex2(View v){
 
     }
}
 
3.開新的activity=>Ex2,抓版面,文字要用text view,這邊text view因為沒有要在java程式中用到,可以不用給id
 
4.onClick函式給名稱,寫程式碼指定變數和函式
public class Ex2 extends AppCompatActivity {
Button home1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex2);
}
public void home(View v){
}
}
 
 
5.撰寫兩個java的onClick method,測試
MainActivity.java
public void ex2(View v){
    Intent in=new Intent();
    in.setClass(MainActivity.this,Ex2.class);
    startActivity(in);
}
 
Ex2.java
public void home(View v){
    Intent in=new Intent();
    in.setClass(Ex2.this,MainActivity.class);
    startActivity(in);
}
**說明:
1.因為Intent的功能都沒有static,所以一定要new
2.startAcitvity(in)=>啟動Intent物件(in)
3. setContentView(R.layout.activity_ex2); 代表要去R下面找一個class layout裡面的activity_ex2.xml的內容給java檔,所以如果這個 activity_ex2位置指定錯會有問題,如果整行都被刪掉,整個頁面都會空白,因為抓不到xml的內容
 


=====================其他功能說明=======================
 
●Activity和xml彼此都無法理解對方的內容,透過R.java(資源檔)去連結
=>xml元件java看不懂,所以所有的原件都註冊在R.java,這個檔會記住所有元件並給一個位址代號,當Activity.java要使用元件時,就會去找這個元件的編碼(序號)
 
●如果要查api,可以到官網直接搜尋reference
 
●要import的時候,可以直接用精靈,如下例,要import Button,先在下面打But打到一半精靈會跳出來,選擇正確的路徑
 
=>就會自動帶出import路徑=>import android.widget.Button;
 
●做好在手機安裝測試後,就會在C:\Java\17\App_2\app\build\outputs\apk路徑下產生一個.apk檔,這就是可以給別人使用的安裝檔
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●實作練習
=>首頁有intent button點了會換頁,換頁後有home button在跳回首頁
 
1.同一個專案開第二頁=>左邊專案按右鍵,
new=>activity=>empty activity,取名為intent,一次產生兩個檔案
intent.java/activity_intent.xml
 
2.在main.xml的intent button修改變數名稱(ID)btnIn,
再給onclick一個函數名稱BtnIn=>android中的變數名稱就是ID
 
3.切到main的activity,寫程式碼
public class MainActivity extends AppCompatActivity {
    Button btnIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnIn = (Button) findViewById(R.id.btnIn);
    }
 
    public void BtnIn(View v) {
        Intent in = new Intent();
        in.setClass(MainActivity.this,intent.class);
        startActivity(in);
    }
}
**說明:
1.先定義一個Button物件,這邊定義的變數名稱可以和ID同名
2.初始化 (即把R.java中元件的位置指給變數btnIn)
=> btnIn = (Button) findViewById(R.id.btnIn); 
=>把Button物件和btnIn這個按鈕在啟動時掛在一起,但因為是用View找到的(類型為View),如果前面沒有加上(Button)去做轉型,就會出現如下錯誤訊息
3.寫onclick函式public void Btnln(View v){}
=>因為是由View抓來的,所以在等View的訊息傳來再執行這個method
4. Intent in = new Intent();
=>new 一個Intent物件,Intent可以用來換頁(指從一個.java換到執行另外一個.java,亦即從一個*.class換到另一個 *.class檔)
5.換頁要使用Intent的setClass()方法,要帶入兩個引數(本身的class,要切換過去的class檔)
=>本身的寫法: MainActivity.this;要切換過去的class檔為:intent.class
6. startActivity(in);=>用這個方法啟動Intent
 
**注意:
1.如果自己的作的class檔名和Intent物件名稱相同,在new Intent物件的時候要注意路徑指向是否正確,不然會new到自己的class檔
2.假設完成後run一直閃退(閃退就是Exception強迫中斷的結果),檢查View有沒有抓到,另外檢查ID或onclic的函式名稱是否有寫錯(大小寫要完全一樣),因為這邊只設定了兩個名稱
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

●開啟新專案與Android Studio基本說明
1.專案名稱的第一個英文字母要大寫
2.選擇開發版本,通常用4開頭的測,ok在寫一個5的,因為怕用6很多手機不能用,wear=>穿戴裝置
3.android會有兩個檔案
layout=>網頁擴充:.xml
activity=>寫程式:.java
 
4.檔案管理:
程式檔=>java/第一個資料夾/MainActivity
介面檔=>java/res/layout/activity_main.xml
*Gradle Scripts函式庫連結檔
*values基本設定值
*drawable放圖片
 
5.設定 file=>settings
appearance,改變整個操作面板的字型=>override default fonts
 
改變程式碼編輯區的字型=>Editor=>Font=>要另存一個設定檔(Save As...)才能改字型和其他設定
android studio啟動後的執行程式是oncreate,就像java中的main程式
 
 
6.排版有分設計模式(Design)和文字模式(text)
 
7.使用的元件會出現在左下角的樹狀元件區
 
8. android studio啟動後的執行程式是onCreate,就像java中的main程式,要執行的內容都要放進去
@override,代表onCreate是繼承AppCompatActivity的onCreate的抽象類別然後把它override
 
9.不同的檔案管理方式切換方法
 
●實作
1.用LinearLayout拖曳進畫面,component tree產生一個LinearLayout,並加入一個button
 
2.切換到text模式,點到哪一項可以在右邊預覽看
 
3.寫法=>android:屬性名稱="值"
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="測試"
    android:textSize="20sp"
    android:textColor="#193283"/>
 
 
4.如果換顏色,可以先輸入顏色碼#000000,直接點左邊的色盤選顏色
 
5.android 手機開啟開發人員選項
=>點版本號碼直到出現你已成為開發人員,然後進到開發人員選項,啟動usb偵錯
*要安裝手機usb驅動程式
 
6.按鈕寬度設定=>fill_parent(全部填滿)
android:layout_width="fill_parent"
 
*android的一個xml(頁面)都有一個activity(專屬的程式檔案)
*android的全部元件一定都會先給ID=>位址序號,這些全部寫在一個檔案R.java
 R.java:記錄所有ID元件、drawable.... (用內部類別/static紀錄=>所以可以直接寫成R.id)
  =>所有的ID元件都記錄在class R下的class ID中,存在同一個檔案中,所以即使不同頁面,ID也不能重複
 
文章標籤

muchone 發表在 痞客邦 留言(0) 人氣()

«123