안드로이드

 

 

 

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;
        }
        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" />
    </LinearLayout>




</RelativeLayout>

 

ListDemo1
package com.example.choi.ex02;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListDemo1 extends ListActivity {
    //1. 변수 선언
    TextView txtResult;
    String[] items={"사과", "포도", "레몬", "수박", "바나나", "체리"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list1);
        //2.리소스 연결
        txtResult =(TextView)findViewById(R.id.textResult);
        //3.집합데이터 =>아답타 =>리스트뷰(집합뷰)로 변환
        // new ArrayAdapter<자료형>(현재화면, 출력옵션, 출력데이터)
        //android.R.java =>안드로이드 내장 R class
        //아답터 생성
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(
          this, android.R.layout.simple_list_item_1, items);
        // 아답터 적용
        setListAdapter(adapter);
    }

    //Alt+Insert
    //Override Methods
    //리스트뷰의 아이템을 선택할 때 호출
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        //Toast :팝업 메시지
        //position - 리스트뷰의 아이템 인덱스
        //Toast.makeText(출력할 액티비티, 출력할 메시지, 옵션).show()
        //Toast.LENGTH_SHORT : 1~2초
        //Toast.LENGTH_LONG : 2~3초
        Toast.makeText(this, position+"번째 항목", Toast.LENGTH_SHORT).show();

        super.onListItemClick(l, v, position, id);

    }



}

 

list1.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/list1"
    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.ListDemo1">

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_centerVertical="true"
        />


</RelativeLayout>

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

그대의 길을 가라. 남들이 무엇이라 하든 내 버려 두어라. -단테

댓글 ( 5)

댓글 남기기

작성