LoginSignup
1
1

Java初心者が簡単な性格診断アプリを作る

Last updated at Posted at 2021-10-15

はじめに

職業訓練校5か月目、授業内容はC言語を使った組み込みから、Java言語での開発に移り変わりました。
今回は授業で制作した性格診断アプリを紹介していきます。

なお、作成するにあたってこちらのサイト様を参考にさせて頂きました。
チェックボックスのチェック状態を設定, 取得する

完成品がこちら

かいつまんで説明すると、チェック一つでカウンターが(いい人度)プラス1されていき、診断開始ボタンを押すと何個チェックしたか(いい人度)結果が表示されるようになっています。

開発環境

Windows10
AndroidStudio(Java言語)

プログラム

MainActivity

package com.example.diagnosis;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void  bt_Start(View view) {
        int cnt = 0;
        CheckBox checkBox;
        boolean isChecked;
        Intent intent = new Intent(MainActivity.this, SubActivity.class);
        checkBox = (CheckBox) findViewById(R.id.cd1);//チェックボックス1を取得
        isChecked = checkBox.isChecked();//チェックボックスにペケが入っていたらisCheckedにtrue
        if(isChecked){
            cnt++;//画面遷移後に表示する数値
            checkBox.setChecked(false);//チェックボックスのペケを外し初期化する
        }
        checkBox = (CheckBox) findViewById(R.id.cd2);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd3);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd4);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd5);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd6);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd7);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd8);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd9);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        checkBox = (CheckBox) findViewById(R.id.cd10);
        isChecked = checkBox.isChecked();
        if(isChecked){
            cnt++;
            checkBox.setChecked(false);
        }
        intent.putExtra("point" , cnt);
        cnt = 0;//intentにcntは渡したので初期化
        startActivity(intent);
    }
}

SubActivity

package com.example.diagnosis;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class SubActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        Intent intent = getIntent();
        int point = intent.getIntExtra("point" , -1);
        TextView textView = (TextView) this.findViewById(R.id.subText);
        textView.setText(point * 10 + "%");
    }
    public void bt_End(View view){
        finish();
    }
}

さいごに

画面遷移、これを使うとぐっとアプリらしくなる。
チェックボックス、ラジオボタン、etc...どのボタンを使えば使いやすいアプリになるか、そのあたりも初期の段階で考えないといけないと思いました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1