androidSutudio 様々なレイアウト部品

Button

  • View.OnClickListenerを実装させる
  • setOnClickListener(class)でボタンが押される準備をする
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

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

        Button 変数名= findViewById(R.id.button);
        変数名.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
  ボタンが押されたときに行いたい処理 ;
    }
}

cheackBox

  • CompoundButton.OnCheckedChengeListenerを実装させる
  • setOnCheckedChangeListener(class)で準備
  • isChecked()で選択状態の取得

RadioButton

  • RadioGroup.OnCheckedChangeListenerを実装させる
  • setOnCheckedChangeListenerで準備
  • onCheckedChanged(RadioGroup group, int checkedId)第二引数にint型でチェックされたボタンIDを返す
  • getCheckedRadioButtonId()でもチェック状態のボタンIDを取得できる
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    RadioButton radioBtn[] = new RadioButton[4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup rgp = findViewById(R.id.group);  //RadioGroupのID取得
        rgp.setOnCheckedChangeListener(this);
      // それぞれのRadioButtonのIDを取得
        radioBtn[0] = findViewById(R.id.radioButton1);
        radioBtn[1] = findViewById(R.id.radioButton2);
        radioBtn[2] = findViewById(R.id.radioButton3);
        radioBtn[3] = findViewById(R.id.radioButton4);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radioButton1:
                Toast.makeText(getApplicationContext(), radioBtn[0].getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton2:
                Toast.makeText(getApplicationContext(), radioBtn[1].getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton3:
                Toast.makeText(getApplicationContext(), radioBtn[2].getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton4:
                Toast.makeText(getApplicationContext(), radioBtn[3].getText(), Toast.LENGTH_SHORT).show();
                break;

        }
    }
}

Spinner

  • AdapterView.OnItemSelectedListenerを実装
  • getSelectedItem()で選択された項目を取得
  • プルダウン項目はstring.xmlにstring-arrayを使って記述
    <string-array name="変数名">
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </string-array>
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spinner = findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

// 選択された項目をstring型の変数に代入
        String 変数名 = String-Arrayの変数名.getSelectedItem().toString();
// トースト表示
        Toast.makeText(getApplicationContext(),string型の変数名, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

SeekBar

  • SeekBar.OnSeekBarChangeListenerを実装
  • onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 引数のprogressでシークバーの現在値の取得
属性値(° ) 向き
90 上から下↓
180 右から左←
270 下から上↑
0 左から右→

記述方法は4つ

  1. 直接レイアウト画面のxmlファイルに記述
  2. 目的のイベントリスナーをインターフェイスとして実装
  3. インナークラスを利用
  4. 無名クラスを利用