'2017/01/09'에 해당되는 글 3건

  1. 2017.01.09 Dynamic Layout Create
  2. 2017.01.09 ToggleButton(버튼2개로)
  3. 2017.01.09 7가지 EventHandler

** Dynamic Layout Create **

 

1. 프로젝트명 : ToggleCustom

2. 정의 : activity_main.xml을 사용하지 않고 java단에서 Layout을 생성하기

3. 화면 :

   

 

4. 안드로이드 버전

     - 스튜디오 : v2.2.3

     - Minumum SDK : API 19:Android 4.4. (KitKat)

 

5. 소스목록

   1. MainActivity.java 

 

6. 소스

 

  1. MainActivity.java

 

 package com.example.farmer.layoutjavacreate;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

        useDynamic();
    }
    //동적으로 Layout및 버튼 생성
    private void useDynamic(){
        //1. Layout 생성
        LinearLayout layoutMain = new LinearLayout(getApplicationContext());
        LinearLayout.LayoutParams paramsLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        layoutMain.setOrientation(LinearLayout.VERTICAL);
        layoutMain.setLayoutParams(paramsLayout);
        //layoutMain.setBackground(Color.parseColor("#ffffff"));

        //2. Button 생성
        Button button01 = new Button(getApplicationContext());
        Button button02 = new Button(getApplicationContext());
        LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        button01.setLayoutParams(paramsButton);
        button02.setLayoutParams(paramsButton);

        layoutMain.addView(button01);
        layoutMain.addView(button02);

        button01.setText("버튼 1");
        button02.setText("버튼 2");

        setContentView(layoutMain);  //layoutMain은 View임
    }
}

 

Posted by 농부지기
,

** ToggleButton(버튼2개로) **

 

 

1. 프로젝트명 : ToggleCustom

2. 버튼 2개를 이용해서 ToggleButton만들기

3. 화면 :

       

 

4. 안드로이드 버전

     - 스튜디오 : v2.2.3

     - Minumum SDK : API 19:Android 4.4. (KitKat)

 

5. 소스목록

   1. MainActivity.java 
   2. activity_main.java
   3. drawable/selector_btn_left.xml                    : 왼쪽 버튼 선택 시
   4. drawable/selector_btn_right.xml                 : 오른쪽 버튼 선택 시
   5. drawable/selector_buttons_text.xml            : 선택된 버튼
   6. drawable/shape_btn_unselected_left.xml   : 왼쪽 버튼 미 선택 시
   7. drawable/shape_btn_unselected_right.xml : 오른쪽버튼 미 선택 시
   8. values/colors.xml                                       : color
 

6. 소스

 

 1. MainActivity.java 

 

package com.example.farmer.togglecustom;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button test1Button;
    Button test2Button;

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

        test1Button = (Button)findViewById(R.id.test1_button);
        test2Button = (Button)findViewById(R.id.test2_button);

        test1Button.setOnClickListener(topButtonsListener);
        test2Button.setOnClickListener(topButtonsListener);
        test1Button.performClick();  //첫번째 버튼을 눌린 효과를 줌
    }

    View.OnClickListener topButtonsListener  = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.test1_button){
                test1Button.setSelected(true);
                test2Button.setSelected(false);
            }
            else{
                test1Button.setSelected(false);
                test2Button.setSelected(true);
            }
        }
    };


}

 

 

  2. activity_main.java

 

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.farmer.togglecustom.MainActivity">

        <Button
            android:id="@+id/test1_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:textColor="@color/selector_buttons_text"
            android:background="@drawable/selector_btn_left"
            android:text="Test 1"/>

        <Button
            android:id="@+id/test2_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:textColor="@color/selector_buttons_text"
            android:background="@drawable/selector_btn_right"
            android:text="Test 2"
            android:layout_weight="1"/>


</LinearLayout>

 

 

 

  3. drawable/selector_btn_left.xml

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@color/gen_app_color" android:state_pressed="true" />
    <item android:drawable="@color/gen_app_color" android:state_selected="true" />
    <item android:drawable="@drawable/shape_btn_unselected_left" />
</selector>

 

 

 

  4. drawable/selector_btn_right.xml

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@color/gen_app_color" android:state_pressed="true" />
    <item android:drawable="@color/gen_app_color" android:state_selected="true" />
    <item android:drawable="@drawable/shape_btn_unselected_right" />
</selector>

 

 

 

  5. drawable/selector_buttons_text.xml

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="@color/gen_app_color"/>
</selector>

 

 

 

  6. drawable/shape_btn_unselected_left.xml

 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <stroke android:width="5px" android:color="#337799" />
        </shape>
    </item>
    <item android:left="5dp"
        android:top="2dp"
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>

  

 

  7. drawable/shape_btn_unselected_right.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="gen_app_color">#40ffe6</color>
    <color name="selector_buttons_text">#ff404a</color>
</resources>

 

 

  8. values/colors.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="gen_app_color">#40ffe6</color>
    <color name="selector_buttons_text">#ff404a</color>
</resources>

 

 

Posted by 농부지기
,

** 7가지 EventHandler **

 

1. 프로젝트명 : ClickEvent

 

2. 정의 : EventHandler 종료

 

3. 소스명 :

    1. MainActivity.java

    2. ClickHandler.java

    3. activity_main.java

 

4. 안드로이드 버전

     - 스튜디오 : v2.2.3

     - Minumum SDK : API 19:Android 4.4. (KitKat)

 

5. 화면

    

 

 

5. 설명

    1. 익명 내부 클래스 처리
    2. 유명 내부 클래스 처리
    3. 유명 내부 클래스
    4. 제3클래스로 처리
    5. 자신의 클래스로 처리
    6. XML에서 핸들러 이벤트 저장
    7. 유명 외부 메소드

 

 

  1. MainActivity.java

 

package com.example.farmer.clickevent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn7;
    EditText et01;

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

        btn1 = (Button)findViewById(R.id.btn01);
        btn2 = (Button)findViewById(R.id.btn02);
        btn3 = (Button)findViewById(R.id.btn03);
        btn4 = (Button)findViewById(R.id.btn04);
        btn5 = (Button)findViewById(R.id.btn05);
        btn6 = (Button)findViewById(R.id.btn06);
        btn7 = (Button)findViewById(R.id.btn07);
        et01 = (EditText)findViewById(R.id.editText01);

        //1. 익명 내부 클래스 : 클래스 이름이 없다.
        //   일회성 : 한번만 쓸 때 사용
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                et01.setText("1. 익명 내부 클래스 처리");
                showToast("1. 익명 내부 클래스 처리");
            }
        });

        //2. 유명 내부 클래스
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et01.setText("2. 유명 내부 클래스 처리");
                showToast("2. 유명 내부 클래스 처리");
            }
        };
        btn2.setOnClickListener(onClickListener);

        //3. 유명 내부 클래스
        btn3.setOnClickListener(new InnerHandler());

        //4. 제3클래스로 처리
        btn4.setOnClickListener(new ClickHandler(MainActivity.this));

        //5. 자신의 클래스로 처리
        //   이건   [public class MainActivity extends AppCompatActivity implements View.OnClickListener]
        //          이렇게 함으로써 처리 됨
        //   아래 [public void onClick(View view) { ] 와 연결 됨
        btn5.setOnClickListener(this);

        //7.
        btn7.setOnClickListener(onClickListener7);
    }

    //3. 유명 내부 클래스
    class InnerHandler implements View.OnClickListener{
        public void onClick(View view) {
            et01.setText("3. 유명 내부 클래스 처리");
            showToast("3. 유명 내부 클래스 처리");
        }
    }

    //5. 자신의 클래스로 처리
    //   이건   [public class MainActivity extends AppCompatActivity implements View.OnClickListener]
    //          이렇게 함으로써 처리 됨
    //   위쪽 [btn5.setOnClickListener(this);] 이 script로 연결됨
    @Override
    public void onClick(View view) {
        et01.setText("5. 자신의 클래스로 처리");
        showToast("5. 자신의 클래스로 처리");
    }

    //6. XML에서 핸들러 이벤트 저장
    public void xmlHandlerMethod(View view){
        et01.setText("6. XML에서 핸들러 이벤트 저장");
        showToast("6. XML에서 핸들러 이벤트 저장");
    }

    //7. 유명 외부 메소드
    private  View.OnClickListener onClickListener7 = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            et01.setText("7. 유명 외부 메소드");
            showToast("7. 유명 외부 메소드");
        }
    };


    public void showToast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

 

 

 

 

  2. CllickHandler.java

 

package com.example.farmer.clickevent;

import android.view.View;

/**
* Created by farmer on 2017-01-07.
*/

public class ClickHandler implements View.OnClickListener {
    MainActivity activity;

    public ClickHandler(MainActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onClick(View view) {
        activity.et01.setText("4. 제3클래스로 처리");
        activity.showToast("4. 제3클래스로 처리");
    }
}

 

 

 

  3. activity_main.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.farmer.clickevent.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:textSize="30dp"
        android:text="OnClickEvent"
        android:id="@+id/textView" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="버튼 1"/>

        <Button
            android:id="@+id/btn06"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="xmlHandlerMethod"
            android:text="버튼 6 (xml)"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 2"/>
    <Button
        android:id="@+id/btn03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 3"/>
    <Button
        android:id="@+id/btn04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 4"/>
    <Button
        android:id="@+id/btn05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 5"/>
    <Button
        android:id="@+id/btn07"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 7"/>

    <EditText
        android:id="@+id/editText01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top"
        android:editable="false"
        android:hint="hint"/>
</LinearLayout>

 

Posted by 농부지기
,