AndroidStudio

9.1 quiz app / Linear layout 내부에 추가(버튼추가) / 코딩

◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e 2020. 8. 15. 01:16
반응형

1.string 설정(extract string resource)

text color, text size

2.another Linear layout 내부에 추가(버튼도 추가)

<LinearLayout>

           <LinearLayout>

                    <Button 두개 추가........./Button>

            </LinearLayout>

</LinearLayout>

3. xml 연결(자바로 가자)

4. click시 버튼 반응설정 

falseButton.setOnClickListener(this); //click 한다면 this

-> make implement 클릭해서 implement method 설정.

 

참고)

text상자 크기(padding) : dp

marginTop : dp

text 사이즈 : sp

 

코딩

<xml>

<?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=".MainActivity"
    tools:ignore="ExtraText"
    android:background="@color/colorPrimaryDark">

    <ImageView
        android:src="@drawable/ic_launcher_foreground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/answer_text_view"
        android:text="@string/question_declaration"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/true_button"
            android:text="@string/true_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/false_button"
            android:text="@string/false_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

<mainActivity> 자바

package com.example.truecitizenquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener { //implement설정됨.

    private Button falseButton;
    private Button trueButton;
    private TextView questionTextView;              //variable 설정


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

        falseButton = findViewById(R.id.false_button);      // xml 연결
        trueButton = findViewById(R.id.true_button);
        questionTextView =findViewById(R.id.answer_text_view);

        falseButton.setOnClickListener(this);           //click 한다면 this로 -> make implement 클릭
        trueButton.setOnClickListener(this);
    }

    @Override                               //click시 method(->this) implement 설정.
    public void onClick(View view) {
        switch(view.getId()){                  //ID가 있다면
            case R.id.false_button:
                Toast.makeText(MainActivity.this,"False",Toast.LENGTH_SHORT).show();
                break;

            case R.id.true_button:
                Toast.makeText(MainActivity.this, "true", Toast.LENGTH_SHORT).show();

        }
    }

}