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);
        }
    }

intent用に画面を作成する

<?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"
    android:gravity="center"
    tools:context=".MyResult">

    <TextView
        android:text="〇〇さんの点数は"
        android:textSize="16sp"
        android:layout_marginBottom="24dp"
        android:id="@+id/nameLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:text="90点でした"
        android:textSize="32sp"
        android:textStyle="bold"
        android:layout_marginBottom="24dp"
        android:id="@+id/scoreLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</LinearLayout>

alertDialogを使ってみよう

AlertDialog.builderとして、setTitle,setMessage,setPoisitiveButtonを設定していきます。

if (myName.equals("")){
        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 {

        }

かっけーーーー!!!!!!!!!

setError, toast, alertDialogだったら、断トツでalertDialogがいいな。

toastを表示する

toastの場合、Toast.makeTextとしてますね。

 if (myName.equals("")){
            // エラー処理
            // myEditText.setError("Please enter your name!");
            Toast.makeText(
                    this,
                    "please enter your name!",
                    Toast.LENGTH_LONG
            ).show();
        } else{

        }

setErrorで、EditTextがnullの場合にerror表示する

onClickでgetScoreを実行します。
nullの場合、EditTextにsetErrorします。

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!");
        } else{

        }
    }

OK!わかってきた