늑대
접속 : 4944   Lv. 64

Category

Profile

Counter

  • 오늘 : 69 명
  • 전체 : 182999 명
  • Mypi Ver. 0.3.1 β
[일상] 혹시 안드로이드 언어 잘하시는분 계신가요? (21) 2015/04/20 AM 09:47
하..지금 공부하고 있는데

제 뇌로는 한계가 있네요..

현재 if(v.getId()==R.id.button4){

}

이 버튼4 라는 곳안에 append 기능을 쓰고 싶거든용..

예를 들어 1을 입력해서 버튼4를 누르면 1뒤에 똑같이 복사가 되서 11
ㅎ을 입력하고 버튼4를 누르면 ㅎㅎ 가 되는 기능을 구현하고 싶거든요..ㅠㅠ


"똑같은 문자를 복사해서 뒤에 붙이기"인데

append 기능을 쓰라는데 코딩구현을 못하겠네요..잼병이라..

혹시 아시는 분 계신가요?

신고

 

Erevos    친구신청

네 이제 저 대괄호 안에 edittextview 나 textview의 변수를 가져오시고 edit.append("4"); 하시면 4가 추가로 들어가요

늑대    친구신청

그 변수라는게 editText01 = (EditText)findViewById(R.id.editText);
이렇게 레이아웃을 연결해주는 걸 말하시는건가요?

Erevos    친구신청

뒤에 번호가 있는게 여러개인것 같은데 글자를 보여줄 레이아웃이 editText01라면 editText01.append("넣을말");
이렇게하면 뒤에 붙어서 들어갑니다.

늑대    친구신청


if(v.getId()==R.id.button4){


editText01 = (EditText)findViewById(R.id.editText);
editText01.append("2");





}

이렇게만 하면될까요?ㅎㅎ;

근데 뭐가 문제인지...클릭을해도 아무 반응이없네요 ㅠ

작전개시!    친구신청

editText01.append(editText01.getText());
하면 되지 않을까요??

늑대    친구신청

이것도 해보았으나...반응이 없어요..ㅠ

세느비엔느.    친구신청

혹시 안되시면
string str = editText01.getText();
str.append("2");
editText01.setText(str);

이런식으로 해보시면...

늑대    친구신청

안되네요 ㅠㅠ

늑대    친구신청

public class MainActivity extends ActionBarActivity implements View.oncl-ckListener {

private static final String LOCAL_FILE = "memo.txt"; //String class 모든 인스턴스가 한 개만 공유,final로 인해 두 번 생성 하지 않음.
EditText editText01; //editText 객체 생성
Button bt01,bt02,bt03,bt04; //버튼 객체 생성


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

-nputStream in;
OutputStream out;
BufferedReader reader;


editText01=(EditText)findViewById(R.id.editText);
bt01=(Button)findViewById(R.id.button); //저장 버튼 레이아웃 연결
bt02=(Button)findViewById(R.id.button2);//삭제 버튼레이아웃 연결
bt03=(Button)findViewById(R.id.button3);//종료 버튼 레이아웃 연결
bt04=(Button)findViewById(R.id.button4);//설정값 버튼 레이아웃 연결.
bt01.setoncl-ckListener(this); //저장 버튼 리스너 등록
bt02.setoncl-ckListener(this); //삭제 버튼 리스너 등록
bt03.setoncl-ckListener(this);//종료 버튼 리스너 등록
bt04.setoncl-ckListener(this);//설정값 버튼 리스너 등록

try {
in = openFile-nput(LOCAL_FILE);
}catch (FileNotFoundException e){

in = getResources().openRawResource(R.raw.data);
}

editText01 = (EditText)findViewById(R.id.editText); //editText.레이아웃 연결.

try {
reader = new BufferedReader(new -nputStreamReader(in,"UTF-8"));
String s;
while ( (s = reader.readLine()) != null) { //한 줄씩 읽어서 처리.
editText01.append(s); //s 객체를 첨부
editText01.append("\n");

}

}catch (IOException e){

Log.e("LOCAL_FILE",e.getMessage());
}


try {
reader = new BufferedReader(new -nputStreamReader(in,"UTF-8"));
String temp;
while ( (temp = reader.readLine()) != null) { //한 줄씩 읽어서 처리.
editText01.append(temp); //s 객체를 첨부
editText01.append("\n");

}

}catch (IOException e){

Log.e("LOCAL_FILE",e.getMessage());
}





}



@Override
public void oncl-ck(View v) {

if(v.getId() == R.id.button2){
editText01.setText(""); //텍스트 초기화

}
if(v.getId() == R.id.button3){
finish(); //종료.
}
if(v.getId() ==R.id.button){

editText01 = (EditText)findViewById(R.id.editText);// editText에 레이아웃 연결
String s = editText01.getText().toString(); //텍스트 추출
if(s.length() == 0){
deleteFile(LOCAL_FILE);
return;

}
try{
OutputStream out = openFileOutput(LOCAL_FILE,MODE_APPEND);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out,"UTF-8")); //특정한 문자단위 작성
writer.append(s);
writer.close();

}
catch (IOException e){
Log.e("Local File", e.getMessage());
}



if(v.getId()==R.id.button4){
editText01 = (EditText)findViewById(R.id.editText);
editText01.append(editText01.getText());





}
}

}

protected void onPause(){ //안드로이드에서 다른 액티비티를 실행할 때 호출되는 메소드
super.onPause();

}


}

전반적인 코드입니다 뭐가 문제일까요?ㅠㅠ

단기알바    친구신청

editText01 = (EditText)findViewById(R.id.editText);// editText에 레이아웃 연결
같은경우 onCreate에서 한번 전역으로 생성하셨는데 이벤트리스너 받을때마다 새로 생성하시는 이유가 있나요?

늑대    친구신청

editText01 = (EditText)findViewById(R.id.editText);는 님 말씀을 따라서 ㅎㅎ
나머지는 삭제하고 하나만 냄겨뒀습니다..
버튼123은 제대로 작동되는데 버튼4만 유독 안되네요..orz

단기알바    친구신청

코드상 리스너 등록은 문제가 없어 보입니다만, 4번이 안된다기 보단 4번안에 코드가 동작 안하는거 같아요. 정확하게 얘기하자면 동작 안한다기 보다 매번 버튼이 눌릴때 editText01을 새로 생성해서 초기화 되는 문제 같습니다.

늑대    친구신청

네..그 4번 버튼 코딩을 못하겠네요..

위에 분들 말씀대로 해보기도 했는데...ㅠㅠ

단기알바    친구신청

if(v.getId()==R.id.button4){
editText01 = (EditText)findViewById(R.id.editText);
editText01.append(editText01.getText());





}
이 코드 안에

editText01 = (EditText)findViewById(R.id.editText);

이 문장을 지우고 해보세요. 다시 객체를 생성 하고 있습니다.

늑대    친구신청

그래도 반응이 없네요..orz..뭐가 문제인지...

중고플스    친구신청

이것은 안드로이드보다 자바의 기초에 대해서 공부를 좀 하셔야 할 것 같은데...
editText는 객체일 뿐이고... Text를 얻어오고 바꾸고 싶다면,
editText.getText(), editText.setText()를 이용하셔야 합니다.
getText()로 기존 값을 알아내서, 뒤에 문자열을 붙이고, setText로 다시 집어 넣는 것이죠.

늑대    친구신청

네 ㅠㅠ 자바 기초가 부족하다보니 이런 불상사가 발생하네요..

작전개시!    친구신청

onCreate 메서드에서 editText01 = (EditText)findViewById(R.id.editText);?는 한번만 해주면되구요
다른곳에서 재정의 할 필요 없습니다.
그리고 oncl-ck 메서드에서 if 문 말고 switch 문으로 수정해주세요 코드가 훨씬 간결해 질거구요 oncl-ck 내에 Log로 콘솔 찍어보시던가 토스트 메시지 찍어보셔서 버튼이 잘 눌리규 있는지 확인부터 해보시기 바랍니다

늑대    친구신청

넵! 우선 중복되는 건 지우고 확인해보겠습니당@

단기알바    친구신청

추가 덧글을 달자면 정답은 없습니다만 oncl-ck 메서드 안에서는
@Override
public void oncl-ck(View v) {

switch (v.getId()) {
case R.id.button:
break;
}
}

이런식으로 스위치 케이스문을 사용하시면 코드 가독성이 올라갑니다.

늑대    친구신청

제가 아직은 초보라서 if문이 편하네요..ㅎㅎㅎ; 자바에 대한 기초가 없다보니..이거; 그냥 첩첩산중이네요
X