郵便番号の正規表現

String str = "333-5555";
Pattern p = Pattern.compile("^[0-9]{3}-[0-9]{4}$");

ハイフンがない固定電話の電話番号なら、

Pattern p = Pattern.compile("^[0-9]{10}$");

携帯と固定電話の混入ならワイルドカード

Pattern p = Pattern.compile("^[0-9]*$");

ハイフンと数字なら、ハイフンをエスケープする

Pattern p = Pattern.compile("^[0-9\\-]+");

これをMainActivity.javaに入れてEditTextの判定したい。

java正規表現2

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Playground {
    public static void main(String[ ] args) {
        
        String str = "123A5";
        
        Pattern p = Pattern.compile("^[0-9]*$");
        Matcher m = p.matcher(str);
        
        System.out.println(m.find());
    }
}

falseになります。
Pattern p = Pattern.compile(“^[0-9]”); とすればtrue
123の次にAが入っているので、Pattern p = Pattern.compile(“^[0-9a-zA-Z]*$”);とすれば、trueに変わります。

if else文で書きます。

public static void main(String[ ] args) {
        
        String str = "123A5";
        
        Pattern p = Pattern.compile("^[0-9a-zA-Z]*$");
        Matcher m = p.matcher(str);
        
        if(m.find() == true){
            System.out.println("OK");
        } else{
            System.out.println("NG");
        }
        
    }

javaの正規表現


class Playground {
    public static void main(String[ ] args) {
        
        String str = "東京都千代田区 123-4567";
        System.out.println(str.matches(".*[0-9]{3}-[0-9]{4}.*"));
        
    }
}

何故だ?
..\Playground\:7: error: unmappable character for encoding Cp1252
String str = “µ?▒Σ║¼Θâ╜σ?âΣ╗úτö░σî║ 123-4567”;
^
..\Playground\:7: error: unmappable character for encoding Cp1252
String str = “µ?▒Σ║¼Θâ╜σ?âΣ╗úτö░σî║ 123-4567”;
^
2 errors

javaのif else

else if() で書きます。

public void getScore(View view){
        // edittextを取得
        EditText fEditText = (EditText) findViewById(R.id.firstnEditText);
        EditText lEditText = (EditText) findViewById(R.id.lastnEditText);
        EditText pEditText = (EditText) findViewById(R.id.phoneEditText);
        EditText mEditText = (EditText) findViewById(R.id.mailEditText);
        // edittextの中身を取得
        String firstName = fEditText.getText().toString().trim();
        String lastName = lEditText.getText().toString().trim();
        String myPhone = pEditText.getText().toString().trim();
        String myMail = mEditText.getText().toString().trim();

        // 中身を観て条件分岐
       if (firstName.equals("")){
             fEditText.setError("Please enter your first name!");
        } else if(lastName.equals("")){
           lEditText.setError("Please enter your last name!");
        } else if(myPhone.equals("")){
           pEditText.setError("Please enter your phone number!");
        }else if(myMail.equals("")){
           mEditText.setError("Please enter your first name!");
        } else {
            Intent intent = new Intent(this, MyResult.class);
            intent.putExtra(EXTRA_MYNAME, firstName);
            startActivity(intent);
        }
    }

androidだとbuildに時間がかかるので、javaのplaygroundで関数の挙動を確かめながらやった方が速いですね。
https://code.sololearn.com/cVRUy2BwauK8/#java

電話番号とメールはバリデーションをかけたい。phoneはtoString? 電話だからstringでいいのか?

EditTextで入力フォームをつくる

ボタンの右寄せは layout_gravity=”right” を使う

<?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=".MainActivity">

    <EditText
        android:id="@+id/firstnameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="your first name"
        />
    <EditText
        android:id="@+id/firstnameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="your last name"
        />

    <EditText
        android:id="@+id/phoneEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="your phone number"
        />

    <EditText
        android:id="@+id/mailEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="your mail address"
        />

    <Button
        android:id="@+id/button"
        android:onClick="getScore"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="submit"
        />

</LinearLayout>

ここまではOKですね。

activityのlabelとparentActivityNameの設定

app -> manifest -> AndroidManifest.xmlの中で、activityのlabelと元へ戻る(parentActivityName)を設定できます。

<activity android:name=".MainActivity"
                  android:label="名前診断">
            <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="com.capital.scoreapp.MainActivity"
                  android:label="結果"
            >
        </activity>

Nice!
parentActivityNameで元へ戻るですか。
android:labelはhtmlのtitleみたいなもんですね。

javaのrandomGenerator

randomGeneratorのclassを使う
randomGenerator.nextInt(101)で0~100のランダムな数を生成する
int型をstring型にキャストする場合は、String.valueOfを使う

        Random randomGenerator = new Random();
        int score = randomGenerator.nextInt(101);
        TextView scoreLabel = (TextView) findViewById(R.id.scoreLabel);
        scoreLabel.setText(String.valueOf(score) + "点!");

OKのようです。phpで作っていく場合は、ほとんど挙動をつくってからstylingでしたが、androidの場合は、stylingにちかい記述(layoutのxml)をはじめっから書いてしまう、というのが進め方として違いますね。

遷移先でのintentの取得方法

intent.getStringExtra(MainActivity.Key) で値を取得する。いわゆるphpの$_POST(“hoge”)みたいなイメージ
textviewに入れ込むには、findViewById にsetText
Javascriptのdocument.ElementGetByIdにそっくりなので、覚えやすいですね。

Intent intent = getIntent();
        String myName = intent.getStringExtra(MainActivity.EXTRA_MYNAME);
        TextView nameLabel = (TextView) findViewById(R.id.nameLabel);
        nameLabel.setText(myName + "の点数は...");

値を引き継いでいます。

.putExtraでEditTextのvalueとkeyを渡す

intent.putExtra(key, myEditText.getText().toString().trim())
引数1は、packageと名前をつけることが推奨されているので、public finalで、定義する。

public final static String EXTRA_MYNAME = "com.capital.scoreapp.myname";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void getScore(View view){


        // edittextを取得
        EditText myEditText = (EditText) findViewById(R.id.myEditText);
        // edittextの中身を取得
        String myName = myEditText.getText().toString().trim();
        // 中身を観て条件分岐
       if (myName.equals("")){
//            // エラー処理
             myEditText.setError("Please enter your name!");
//            Toast.makeText(
//                    this,
//                    "please enter your name!",
//                    Toast.LENGTH_LONG
//            ).show();
//        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
//        alertDialogBuilder
//                .setTitle("Error!")
//                .setMessage("Please enter your name!")
//                .setPositiveButton("OK", null);
//        AlertDialog alertDialog = alertDialogBuilder.create();
//        alertDialog.show();
        } else {
            Intent intent = new Intent(this, MyResult.class);
            intent.putExtra(EXTRA_MYNAME, myName);
            startActivity(intent);
        }
    }