본문 바로가기

개발/모바일

stanford CS 193A 16sp Lecture 03: More widgets and lists

반응형

강의 링크 : https://www.youtube.com/watch?v=0R98iUX5Wxo&index=3&list=PLYKXDWkoIMUH088iBEr_B2WPfbPwtaG-V


xml : declare the thing

java code : handle events


1. ImageButton : clickable -> event

이미지 파일은  /res/drawable에 넣음

key attributes

android:clickable = "true"



2. ImageView + RadioButton + Toast or Log

package com.example.han.lec3ninjaturtles;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

public void sherlockClick(View view){
ImageView img = (ImageView)findViewById(R.id.targetImageView);
// 어떤 버튼을 눌렀는지 찾은 후에 스크린에 해당 사진을 보여줌
if(view.getId()==R.id.oneButton){
img.setImageResource(R.drawable.sherlock1);
Toast.makeText(getApplicationContext(), "one", Toast.LENGTH_SHORT).show();
}else if(view.getId()==R.id.twoButton){
img.setImageResource(R.drawable.sherlock2);
Toast.makeText(getApplicationContext(), "two", Toast.LENGTH_SHORT).show();
}else if(view.getId()==R.id.threeButton){
img.setImageResource(R.drawable.sherlock3);
Toast.makeText(getApplicationContext(), "three", Toast.LENGTH_SHORT).show();

}
}

public void onClick(View view){
EditText editText = (EditText)findViewById(R.id.input_box);
String text = editText.getText().toString();
Toast.makeText(this, text , Toast.LENGTH_SHORT).show();
Log.d("toast", "user typed "+text);
}

} 

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.example.han.lec3ninjaturtles.MainActivity">

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<RadioButton
android:id="@+id/oneButton"
android:text="sherlock1"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sherlockClick"/>
<RadioButton
android:id="@+id/twoButton"
android:text="sherlock2"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sherlockClick"/>
<RadioButton
android:id="@+id/threeButton"
android:text="sherlock3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sherlockClick"/>
</RadioGroup>


<ImageView
android:id="@+id/targetImageView"
android:src="@drawable/sherlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_box"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="OK"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>



3. static list

res/values/strings.xml 에 정의

 <resources>

    <string name="app_name">Lec3NinjaTurtles</string>

<string-array name="sherlocks">
<item>sherlock1</item>
<item>sherlock2</item>
<item>sherlock3</item>
</string-array>
</resources>

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.example.han.lec3ninjaturtles.MainActivity">

<ListView
android:entries="@array/sherlocks"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>

</LinearLayout>


 


4. dynamic list


- dynamic list를 만들 때는 adapter가 필요함..

- listView에서는 OnClick이 안먹힘! attachment 부분을 xml이 아닌 java에 넣어야 함


 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.example.han.lec3ninjaturtles.MainActivity">
<TextView
android:id="@+id/definition"
android:text="click on word"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/word_list"
android:entries="@array/sherlocks"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>

</LinearLayout>

 package com.example.han.lec3ninjaturtles;


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 java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
// list of word/definition pairs for game
private static final String[] WORDS = {
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up",
"abrogate", "repeal or annul by authority",
"abstruse", "difficult to comprehand",
"acarpous", "effect no longer fertile worn out",
"accretion", "the growing of seperate things into one",
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up",
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < WORDS.length; i+=2){
list.add(WORDS[i]);
}

//listView를 위한 adapter를 만들어야 함
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
/* new ArrayAdapter<String>(activity, layout, array);
* activity : 주로 this
* layout : default는 android.R.layout.simple_list_item_1
* get array by reading your file or data source of choice
* */
ListView listView = (ListView)findViewById(R.id.word_list);
listView.setAdapter(adapter);

// clickable하게 만들기
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Log.d("list", "the user clicked item"+position);
String definition = WORDS[position*2+1];
TextView definitionView = (TextView)findViewById(R.id.definition);
definitionView.setText(definition);
}
});
}

}



4. 퀴즈 게임 만들기

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.example.han.lec3ninjaturtles.MainActivity">

<TextView
android:id="@+id/word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="word here"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<ListView
android:id="@+id/word_list"
android:entries="@array/sherlocks"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>

</LinearLayout> 

 package com.example.han.lec3ninjaturtles;


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;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
// list of word/definition pairs for game
private static final String[] WORDS = {
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up",
"abrogate", "repeal or annul by authority",
"abstruse", "difficult to comprehand",
"acarpous", "effect no longer fertile worn out",
"accretion", "the growing of seperate things into one",
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up",
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up"
};

private HashMap<String, String> dictionary;

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

dictionary = new HashMap<String, String>();

ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < WORDS.length; i+=2){
list.add(WORDS[i]);
dictionary.put(WORDS[i], WORDS[i+1]);
}


ArrayList<String> fiveWords = new ArrayList<String>();
//shuffle word
Collections.shuffle(list);
for(int i = 0 ; i < 5; i++){
fiveWords.add(list.get(i));
}
// 하나 골라서 문제냄
final String theWord = fiveWords.get(0);
// list of matched definition
final ArrayList<String> fiveDefinition = new ArrayList<String>();
for(String word: fiveWords)
fiveDefinition.add(dictionary.get(word));
Collections.shuffle(fiveDefinition);


//listView를 위한 adapter를 만들어야 함
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fiveDefinition);
ListView listView = (ListView)findViewById(R.id.word_list);
listView.setAdapter(adapter);

TextView word = (TextView)findViewById(R.id.word);
word.setText(theWord);

// clickable하게 만들기
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// new anonymous 어쩌고를 만들면 위에서 변수 선언할 때 final이어야 함
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String defnClicked = fiveDefinition.get(position);
String rightAnswer = dictionary.get(theWord);
if(defnClicked.equals(rightAnswer)) {
/* Toast.makeText(this, "you got it!", Toast.LENGTH_SHORT).show();
* 지금 this는 activity 가 아님 */
Toast.makeText(MainActivity.this, "you got it!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong!", Toast.LENGTH_SHORT).show();
}

}
});
}

}


위의 코드 수정!

 - 리스트의 데이터가 바뀌면 리스트에 알려줘야 함 (list.setAdapter(myAdapter))

package com.example.han.lec3ninjaturtles;

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;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
// list of word/definition pairs for game
private static final String[] WORDS = {
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up",
"abrogate", "repeal or annul by authority",
"abstruse", "difficult to comprehand",
"acarpous", "effect no longer fertile worn out",
"accretion", "the growing of seperate things into one",
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up",
"abate", "to lessen to subside",
"abeyance", "suspended action",
"abjure", "promise or swear to give up"
};

private HashMap<String, String> dictionary;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
private ArrayList<String> fiveDefinition;

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

dictionary = new HashMap<String, String>();
list = new ArrayList<String>();
for(int i = 0 ; i < WORDS.length; i+=2){
list.add(WORDS[i]);
dictionary.put(WORDS[i], WORDS[i+1]);
}
fiveDefinition = new ArrayList<String>();

pickRandomWord();
}



private void pickRandomWord(){
ArrayList<String> fiveWords = new ArrayList<String>();
//shuffle word
Collections.shuffle(list);
for(int i = 0 ; i < 5; i++){
fiveWords.add(list.get(i));
}
// 하나 골라서 문제냄
final String theWord = fiveWords.get(0);
// list of matched definition

fiveDefinition.clear();
for(String word: fiveWords)
fiveDefinition.add(dictionary.get(word));
Collections.shuffle(fiveDefinition);


//listView를 위한 adapter를 만들어야 함
if(adapter==null){
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fiveDefinition);
}else{
adapter.notifyDataSetChanged();
}
ListView listView = (ListView)findViewById(R.id.word_list);
listView.setAdapter(adapter);

TextView word = (TextView)findViewById(R.id.word);
word.setText(theWord);

// clickable하게 만들기
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// new anonymous 어쩌고를 만들면 위에서 변수 선언할 때 final이어야 함
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String defnClicked = fiveDefinition.get(position);
String rightAnswer = dictionary.get(theWord);
if(defnClicked.equals(rightAnswer)) {
/* Toast.makeText(this, "you got it!", Toast.LENGTH_SHORT).show();
* 지금 this는 activity 가 아님 */
Toast.makeText(MainActivity.this, "you got it!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong!", Toast.LENGTH_SHORT).show();
}

pickRandomWord();

}
});
}

} 



반응형