package com.example.dmltj.randombt22;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView resultTx;
String result = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
resultTx = (TextView)findViewById(R.id.textView);
}
});
//imagebutton 생성
ImageButton btn[] = new ImageButton[6];
ImageButton tmp1 = null;
//xml코드내의 imagebutton과 java 코드와의 링크
btn[0] = (ImageButton) findViewById(R.id.imageButton1);
btn[1] = (ImageButton) findViewById(R.id.imageButton2);
btn[2] = (ImageButton) findViewById(R.id.imageButton3);
btn[3] = (ImageButton) findViewById(R.id.imageButton4);
btn[4] = (ImageButton) findViewById(R.id.imageButton5);
btn[5] = (ImageButton) findViewById(R.id.imageButton6);
//난수 생성부분
Random random = new Random();
int location1, location2,location3,location4,location5,location6;
//버튼 주소를 담고있는 배열을 임의의 난수를 통해 무작위로 섞음
//swap을 통한 방식으로 구현
for (int i = 0; i < 6; i++) {
location1 = random.nextInt(6); //난수 발생 -> 이미지 갯수만큼(0, 1, 2, 3, 4)
location2 = random.nextInt(6);
location3 = random.nextInt(6);
location4 = random.nextInt(6);
location5 = random.nextInt(6);
location6 = random.nextInt(6);
tmp1 = btn[location1];
btn[location1] = btn[location2];
btn[location2] = tmp1;
tmp1=btn[location3];
btn[location3]=btn[location4];
btn[location4]=tmp1;
tmp1=btn[location5];
btn[location5]=btn[location6];
btn[location6]=tmp1;
}
//섞인 이미지버튼 배열에 각각 이미지 등록
btn[0].setBackgroundResource(R.drawable.e);
btn[1].setBackgroundResource(R.drawable.f);
btn[2].setBackgroundResource(R.drawable.d);
btn[3].setBackgroundResource(R.drawable.b);
btn[4].setBackgroundResource(R.drawable.c);
btn[5].setBackgroundResource(R.drawable.a);
}
public void onClick1(View target) {
result+="a";
}
public void onClick2(View target){
result+="b";
}
public void onClick3(View target){
result+="c";
}
public void onClick4(View target){
result+="d";
}
public void onClick5(View target){
result+="e";
}
public void onClick6(View target){
result+="f";
}
public void resultClick(View target){
resultTx.setText(result);
}
}
안녕하세요 안드로이드를 처음 배우고 있는 학생입니다.
이번에 영단어 게임 어플을 만들어볼생각인대
이미지버튼이 위치가 서로 섞이고 버튼을 눌러 결과를 출력하려합니다.
예를들어 화면에
E F D
C A B
이런식으로 생성되면 F A C 클릭시 텍스트뷰에 fac가 출력되도록 하고싶은대 버튼을 클릭하고 결과출력을하면
어플이 중지되었다고 뜨면서 꺼집니다.. 어떻게 해결해야할까요?