본문 바로가기

IT프로그래밍

연습) 버튼리스너 방법 1

개인적으로 담아두는 곳입니다.

그냥 소스로 실행하시면 텍스트파일이 없어서 버튼이 눌렀을때 오류가 발생할 듯합니다.


package com.example.llotto;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.StringTokenizer;


import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends Activity {

int a = 0;

String str2 = null;

char cr;

String store[];

int count = 0;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

store = new String[100];

Button button = (Button) findViewById(R.id.button1);

Button button2 = (Button) findViewById(R.id.button2);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

BufferedReader br = null;

StringBuffer sb = new StringBuffer();

StringBuffer sb2 = new StringBuffer();

try

{

br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.lotto_557)));

String str = null;

Log.e("tag", "before while");

while((str = br.readLine()) != null)

{

//cr = str.charAt(0);

sb.append(str+"\n");

StringTokenizer st = new StringTokenizer(str);

       while(st.hasMoreTokens()) //토근이 있는동안 while문이 실행됨

       {   

           String str2 = st.nextToken(); // 토근을 temp 변수에 저장

           System.out.println("temp="+str2);

           a = Integer.parseInt(str2); //a에 str2를 int형으로 변환후 저장 

           store[count] = str2;

           

           sb2.append(store[count]+" ");

           count++;

       }

sb2.append("\n");

}

} catch (Exception e) 

{

// TODO: handle exception

}

TextView textview = (TextView) findViewById(R.id.text);

TextView textview2 = (TextView) findViewById(R.id.text2);

textview.setText(sb.toString());

textview2.setText(sb2.toString());

}

});

button2.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(MainActivity.this, "활성", Toast.LENGTH_SHORT).show();

}

});

}



@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


}