안드로이드

 

 

 

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;
        }

        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" />


    </LinearLayout>




</RelativeLayout>

 

ListDemo2
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 ListDemo2 extends AppCompatActivity {

    //1.변수선언
    TextView textResult;
    ListView list;
    String[] items={"TV", "냉장고", "세탁기", "오디오", "스마트폰" };


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

        //2.리소스 연결
        textResult=(TextView)findViewById(R.id.textView);
        list=(ListView)findViewById(R.id.list);
        //3. 아답터 생성
//new ArrayAdapter<자료형>(출력할 화면, 화면레이아웃, 원본데이터)

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, items) ;
        //배열데이터가 리스트뷰에 바인딩
        list.setAdapter(adapter);
        //4.이벤트 처리
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                Toast.makeText(ListDemo2.this, position+"번째 항목", Toast.LENGTH_LONG).show();

                textResult.setText(items[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)

댓글 남기기

작성