안드로이드

 

 

 

 

 

 

MainActivity
package com.example.choi.ex02;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void onClick(View v){
        Intent intent=null;//인텐트 선언
        switch (v.getId()){ //클릭한 버튼의 아이디
            case R.id.button:
                //현재 화면에서 ListDemo1으로 이동
                intent =new Intent(this, ListDemo1.class);
                break;
            case R.id.button2:
                //현재 화면에서 ListDemo1으로 이동
                intent =new Intent(this, ListDemo2.class);
                break;

            case R.id.button3:
                intent=new Intent(this, ListXml.class);
                break;
        }

        startActivity(intent);
    }


}



 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.choi.ex02.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="ListActivity"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button" />

        <Button
            android:text="리스트(ListActivity2)"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2" />

        <Button
            android:text="리스트-xml"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button3" />


    </LinearLayout>



</RelativeLayout>

 

 

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="friut">
        <item>사과</item>
        <item>수박</item>
        <item>참외</item>
        <item>딸기</item>
        <item>포도</item>
        <item>복숭아</item>
        <item>레몬</item>
        <item>귤</item>
    </string-array>

</resources>

 

ListXml.class

package com.example.choi.ex02;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListXml extends AppCompatActivity {

    //1.변수선언
    TextView textView;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //화면 레이아웃 파일 설정
        setContentView(R.layout.list2);

        //2.리소스 연결
        textView=(TextView)findViewById(R.id.textView);
        list=(ListView)findViewById(R.id.list);

        //3.아답터 생성
        //createFromResource(출력 화면, xml파일, 출력 옵션)

        // 문자열 배열 리소스
         // /res/values/arrays.xml
        // 소스코드상에서 문자열 배열 가져오는 방법
        final String[] friut = getResources().getStringArray(R.array.friut);

        ArrayAdapter<CharSequence> adapter=
                ArrayAdapter.createFromResource(this, R.array.friut,
                        android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(ListXml.this, friut[position] , Toast.LENGTH_SHORT).show();
                textView.setText(friut[position]);
            }
        });

    }



}

 

list2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list2"
    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.choi.ex02.ListDemo2">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp"
        android:layout_marginTop="21dp">

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView" />

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>



</RelativeLayout>

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

인간에게는 세 가지 사건밖에 없다. 태어나고, 살고, 죽는다. 태어나는 것은 느끼지 못한다. 죽는 것은 괴로워한다. 그리고 사는 것은 잊고 있다. -라 브뤼에르

댓글 ( 4)

댓글 남기기

작성