Java packageの書き方

クラス

class User {
  String name = "Me"; // field

  void sayHi(){
    System.out.println("hi!");
  }
}

public class MyApp{

  public static void main(String[] args){

    User tom;
    tom = new User(); // instance
    System.out.println(tom.name);
    tom.sayHi();
  }
}

コンストラクター:クラスの初期化

class User {
  String name;

  // constructor
  User(String name){
    this.name = name;
  }

  // User(){
  //   this.name = "me";
  // }

  User(){
    this("me!");
  }

  void sayHi(){
    System.out.println("hi!" + this.name);
  }
}

public class MyApp{

  public static void main(String[] args){

    User tom;
    tom = new User(); // instance
    System.out.println(tom.name);
    tom.sayHi();
  }
}

クラスの継承とoverride

class User {
  String name;

  User(String name){
    this.name = name;
  }

  void sayHi(){
    System.out.println("hi!" + this.name);
  }
}

class AdminUser extends User {
  AdminUser(String name){
    super(name);
  }
  void sayHello(){
    System.out.println("hello!" + this.name);
  }

  // override
  @Override
  void sayHi(){
    System.out.println("[admin] hi!" + this.name);
  }

}

public class MyApp{
  public static void main(String[] args){

    User tom = new User("tom"); // instance
    System.out.println(tom.name);
    tom.sayHi();

    AdminUser bob = new AdminUser("bob"); // instance
    System.out.println(bob.name);
    bob.sayHi();
    bob.sayHello();
  }
}

public:アクセス可
private:同じクラスならアクセス可能
protected:同じパッケージのみ

パッケージ化
com/hpscript/myapp/MyApp.java

package com.hpscript.myapp;
import com.hpscript.myapp.model.User;
import com.hpscript.myapp.model.AdminUser;
// import hpscript.myapp.model.*;

public class MyApp{
  public static void main(String[] args){

    User tom = new User("tom"); // instance
    // System.out.println(tom.name);
    tom.sayHi();

    AdminUser bob = new AdminUser("bob"); // instance
    // System.out.println(bob.name);
    bob.sayHi();
    bob.sayHello();
  }
}

com/hpscript/myapp/model/User.java

package com.hpscript.myapp.model;

public class User {
  protected String name;

  public User(String name){
    this.name = name;
  }

  public void sayHi(){
    System.out.println("hi!" + this.name);
  }
}

com/hpscript/myapp/model/AdminUser.java

package com.hpscript.myapp.model;

public class AdminUser extends User {
  public AdminUser(String name){
    super(name);
  }
  public void sayHello(){
    System.out.println("hello!" + this.name);
  }

  // override
  @Override
  public void sayHi(){
    System.out.println("[admin] hi!" + this.name);
  }

}

java 構文

条件分岐

int score = 35;
    if (score > 80){
      System.out.println("great");
    } else if (score> 60){
      System.out.println("good");
    } else {
    System.out.println("soso");
  }
int score = 65;
  String msg = score > 80 ? "great" : "soso...";

switch

String signal = "red";
    switch (signal){
      case "red":
        System.out.println("stop!");
        break;
      case "blue":
        System.out.println("go");
        break;
      case "yellow":
        System.out.println("caution");
        break;
      default:
        System.out.println("wrong signal");
        break;

while, do while

int i = 0;
    while (i < 10){
      System.out.println(i);
      i++;
    } 
int i = 0;
    do {
      System.out.println(i);
      i++;
    } while (i < 10);

for

for(int i = 0; i < 10; i++){
        if (i == 5){
          break;
        }
        System.out.println(i);
      }

array

      int[] sales;
      sales = new int[3];
      sales[0] = 100;
      sales[1] = 200;
      sales[2] = 300;
      sales[1] = 1000;
      System.out.println(sales[1]);
      // int[] sales:
      // sales = new int[](100, 200, 300);

      int[] sales = {100, 200, 300}

要素の操作

int[] sales = {700, 300, 500};

      for (int i = 0; i < sales.length; i++){
        System.out.println(sales[i]);
      }

int[] sales = {700, 300, 500};

      for (int sale : sales){
        System.out.println(sale);
      }

参照型にはメモリ領域のバッチが入ります。文字列の場合は、変更不可となります。

      int[] a = {3, 5, 7};
      int[] b = a;
      b[1] = 8;
      System.out.println(a[1]);
      System.out.println(b[1]);
      String s = "hello";
      String t = s;
      t = "world";

メソッド

public static void sayHi(String name){
    System.out.println("hi!" + name);
  }

  public static void main(String[] args){
    sayHi("jack");
  }

public static String sayHi(String name){
    return "hi!" + name;
  }

  public static void main(String[] args){
    String msg = sayHi("jack");
    System.out.println(msg);
  }

オーバーロード

public static void sayHi(){
    System.out.printl("hi! nobody");
  }

  public static void main(String[] args){
    sayHi("jack"); //引数
    sayHi();
  }

open-java JDK8

開発環境にJDKをインストールします。javaは.classにコンパイルすれば、Linux以外のOS(mac, windows等)のJRE(Java Runtime Environment)/JVM(Java Virtual Machine)で実行することが可能です。Run anywhereの思想です。

http://openjdk.java.net/install/

[vagrant@localhost java]$ sudo yum -y install java-1.8.0-openjdk-devel

ターミナルからのコンパイル方法です。

public class MyApp{

  public static void main(String[] args){

    String msg;
    msg = "Hello Wrold again";
    System.out.println(msg);
  }
}
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hello Wrold again

データ型

    char a = 'a';
    int x = 10;
    long y = 5555555555L;
    double d = 23423.344;
    float f = 32.33F;
    boolean flag = true; // false
    // \n \t

演算

// 演算
    int i;
    i = 10 / 3;
    i = 10 % 3;
    int x = 5;
    x++;
    x--;
    int x = 5;
    x = x + 12;
    x += 12;
    String s;
    s = "hello " + "world";

型変換(cast)

    double d = 452323.231;
    int i = (int)d;
    int i = 10;
    double d = i / 4

Android Fragment

Fragmentは、Activity でのユーザー インターフェースの挙動や部位を表すものです。 1 つのアクティビティに複数のフラグメントを組み合わせてマルチペインの UI をビルドしたり、複数のアクティビティでフラグメントを再利用したりできます。

TitlesFragment.java

package self.myfragmentapp;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;


public class TitlesFragment extends ListFragment {

    public TitlesFragment() {}

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                News.Titles
        ));
    }
}

sqliteの設定

ContractとOpenHelperのクラスを作成します。OepnHelperはSQLiteOpenHelperを継承します。
New->Other->ContentProviderから新規にcomponentを作成し、呼び出します。authorityは、packageとContentProvider名です。(self.mymemoapp.MemeoContentProvider)

package self.mymemoapp;

import android.provider.BaseColumns;


public final class MemoContract {

    public MemoContract(){}

    public static abstract class Memos implements BaseColumns {
        public static final String TABLE_NAME = "memos";
        public static final String COL_TITLE = "title";
        public static final String COL_BODY = "body";
        public static final String COL_CREATE = "created";
        public static final String TABLE_UPDATE = "updated";
    }
}
package self.mymemoapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MemoOpenHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "myapp.db";
    public static final int DB_VERSION = 1;

    public static final String CREATE_TABLE =
            "create table memos (" +
                "_id integer primary key autoincrement, "+
            "title text, " +
            "body text, " +
            "created datetime default current_timestamp, " +
            "updated datetime default current_timestamp)";

    public static final String INIT_TABLE =
            "insert into memos (title, body) values " +
                    "('t1', 'b1'), " +
                    "('t2', 'b2'), " +
                    "('t3', 'b3'), ";

    public static final String DROP_TABLE =
            "drop table if exists " + MemoContract.Memos.TABLE_NAME;

    public MemoOpenHelper(Context c){
        super(c, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        db.execSQL(CREATE_TABLE);
        db.execSQL(INIT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        db.execSQL(DROP_TABLE);
        onCreate(db);
    }
}

SQLiteとAndroid

UserOpenHelperでSQLiteOpenHelperを継承します。

package self.mydbapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by narishige on 2016/11/21.
 */

public class UserOpenHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "myapp.db";
    public static final int DB_VERSION = 1;
    public static final String CREATE_TABLE =
            "create table " + UserContract.Users.TABLE_NAME + " (" +
                    UserContract.Users._ID + " integer primary key autoincrement," +
                    UserContract.Users.COL_NAME + "name text," +
                    UserContract.Users.COL_SCORE + "score integer)";
    public static final String INIT_TABLE =
            "insert into users (name, score) values " +
                    "('yoshimoto', 55), " +
                    "('kimura', 45), " +
                    "('igarashi', 82)";
    public static final String DROP_TABLE =
            "drop table if exists users";

    public UserOpenHelper(Context c){
        super(c, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // create table
        sqLiteDatabase.execSQL(CREATE_TABLE);
        // init table
        sqLiteDatabase.execSQL(INIT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        // drop table
        sqLiteDatabase.execSQL(DROP_TABLE);
        // onCreate
        onCreate(sqLiteDatabase);
    }
}

insert, delete, update

        UserOpenHelper userOpenHelper = new UserOpenHelper(this);
        SQLiteDatabase db = userOpenHelper.getWritableDatabase();
        // open db
        ContentValues newUser = new ContentValues();
        newUser.put(UserContract.Users.COL_NAME, "tanaka");
        newUser.put(UserContract.Users.COL_SCORE, "42");
        long newID = db.insert(
          UserContract.Users.TABLE_NAME,
                null,
                newUser
        );
        
        ContentValues newScore = new ContentValues();
        newScore.put(UserContract.Users.COL_SCORE, 100);
        int updatedCount = db.update(
                UserContract.Users.TABLE_NAME,
                newScore,
                UserContract.Users.COL_NAME + " = ?",
                new String[] {"sakai"}
        );
        int deletedCount = db.delete(
          UserContract.Users.TABLE_NAME,
                UserContract.Users.COL_NAME + " = ?",
                new String[] {"sakai"}
        );
        
        Cursor c = null;
        c = db.query(
                UserContract.Users.TABLE_NAME,
                null, // fields
                null, // where
                null, // where arg
                null, // groupby
                null,
                null
        );

トランザクション

 try {
            db.beginTransaction();
            db.execSQL("update users " +
                    "set score = score + 10 " +
                    "where name = 'kaneko'");
            db.execSQL("update users " +
                    "set score = score + 10 " +
                    "where name = 'muraoka'");
        } catch(SQLException e){
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }

ブラウザ

%e7%84%a1%e9%a1%8c

package self.mybrowser;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private WebView myWebView;
    private EditText urlText;

    private static final String INITIAL_WEBSITE = "http://finance.yahoo.co.jp";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.myWebView);
        urlText = (EditText) findViewById(R.id.urlText);

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
                    public void onPageFinished(WebView view, String url){
                getSupportActionBar().setSubtitle(view.getTitle());
                urlText.setText(url);
            }
        });
        myWebView.loadUrl(INITIAL_WEBSITE);
    }

    public void clearUrl(View view){
        urlText.setText("");
    }

    public void showWebSite(View view){
        String url = urlText.getText().toString().trim();

        if (!Patterns.WEB_URL.matcher(url).matches()){
            urlText.setError("Invalid URL");
        } else {
            if(url.startsWith("http;//") && !url.startsWith("https://")){
                url = "http://" + url;
            }
            myWebView.loadUrl(url);
        }
    }

    @Override
    public void onBackPressed(){
        if (myWebView.canGoBack()){
            myWebView.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(myWebView != null){
            myWebView.stopLoading();
            myWebView.setWebViewClient(null);
            myWebView.destroy();
        }
        myWebView = null;
    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="self.mybrowserapp.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_weight="1"
            android:id="@+id/urlText"
            android:layout_width="0dp"
            android:onClick="clearUrl"
            android:inputType="text|textNoSuggestions"
            android:layout_height="wrap_content"/>

        <Button
            android:text="Browse"
            android:onClick="showWebsite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <WebView
        android:layout_weight="1"
        android:id="@+id/myWebView"
        android:layout_width="match_parent"
        android:layout_height="0dp"></WebView>

</LinearLayout>

SystemClockによるタイムウォッチ

SystemClock.elapsedRealtimeからstartTimeを引いた時間を表示しています。
参考:https://developer.android.com/reference/android/os/SystemClock.html

リフェレンス
handler:A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it — from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
SimpleDateFormat:is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

package self.stopwatchapp;

import android.icu.text.SimpleDateFormat;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private long startTime;
    private long elapsedTime = 0l;

    private Handler handler = new Handler();
    private Runnable updateTimer;

    private Button startButton;
    private Button stopButton;
    private Button resetButton;
    private TextView timerLabel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        resetButton = (Button) findViewById(R.id.resetButton);
        timerLabel = (TextView) findViewById(R.id.timerLabel);

        setButtonState(true, false, false);


    }

    public void setButtonState(boolean start, boolean stop, boolean reset){
        startButton.setEnabled(start);
        stopButton.setEnabled(stop);
        resetButton.setEnabled(reset);
    }

    public void startTimer(View view){
        // starttime
        startTime = SystemClock.elapsedRealtime(); //起動からの経過時間

        // 現在の経過時間
        // Handler -> Runnable -> UI
        updateTimer = new Runnable(){
            @Override
            public void run() {
                long t = SystemClock.elapsedRealtime() - startTime + elapsedTime;
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS", Locale.US);
                timerLabel.setText(sdf.format(t));
                handler.removeCallbacks(updateTimer);
                handler.postDelayed(updateTimer, 10);
            }
        };
        handler.postDelayed(updateTimer, 10);

        // button
        setButtonState(false, true, false);
    }
    public void stopTimer(View view){
        elapsedTime += SystemClock.elapsedRealtime() - startTime;
        handler.removeCallbacks(updateTimer);
        setButtonState(true, false, true);
    }
    public void resetTimer(View view) {
        elapsedTime = 0l;
        timerLabel.setText("00:00:000");
        setButtonState(true, false, false);
    }

}

LinearLayoutのverticalとhorizontal

LinearLayoutのvertical中に、LinearLayoutのhorizontalで、tableのようなレイアウトを実現しています。

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="self.stopwatchapp.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:gravity="center"
        android:textSize="64sp"
        android:textStyle="bold"
        android:id="@+id/timerLabel"
        android:text="00:00.000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_height="0dp">

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="Start"
        android:id="@+id/startButton"
        android:onClick="startTimer"
        android:layout_height="wrap_content"
        />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Stop"
            android:id="@+id/stopButton"
            android:onClick="stopTimer"
            android:layout_height="wrap_content"
            />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Reset"
            android:id="@+id/resetButton"
            android:onClick="resetTimer"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
</LinearLayout>

classはbooleanで判定しています。

        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        resetButton = (Button) findViewById(R.id.resetButton);

        setButtonState(true, false, false);


    }

    public void setButtonState(boolean start, boolean stop, boolean reset){
        startButton.setEnabled(start);
        stopButton.setEnabled(stop);
        resetButton.setEnabled(reset);
    }

AndroidManifest.xml

AndroidManifest.xmlのandroid:parentActivityNameで、Activityを元画面に戻します。

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MyForm">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MyResult"
            android:parentActivityName="self.namescoreapp.MyForm"></activity>
    </application>

また、strings.xmlにtextを記載し、activityのxmlからは@string/で呼び出しています。

<EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/myEditText"
        android:layout_weight="1"
        android:hint="@string/name_hint" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getScore"
        android:id="@+id/myButton"
        android:text="@string/button_label" />