안드로이드 개발 질문/답변
(글 수 45,052)
자바파일에 UI생성한것을 XML로 바꾸는 작업을했습니다ㅠㅠ
XML로 바꾼후 아이디값을 불러왔는데..왜 실행이안될까요?ㅠㅠ
셋온클릭리스너 사용을해야할거같은데.. 도와주세요 ㅠㅠ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/startmain">
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:id="@+id/linearLayout1">
<Chronometer android:text="Chronometer" android:id="@+id/mChronometer"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Chronometer>
<LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:id="@+id/linearLayout2">
<Button android:text="start" android:id="@+id/startbutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="stop" android:id="@+id/stopbutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="reset" android:id="@+id/resetButton"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
-----------------------------------------------
public class startmain extends Activity {
Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
mChronometer = new Chronometer(this);
Button startbutton = (Button)findViewById(R.id.startbutton);
Button stopbutton = (Button)findViewById(R.id.stopbutton);
Button resetbutton = (Button)findViewById(R.id.resetButton);
}
private void showElapsedTime() {
long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
Toast.makeText(startmain.this, "Elapsed milliseconds: " + elapsedMillis,
Toast.LENGTH_SHORT).show();
}
OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = mChronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
showElapsedTime();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
showElapsedTime();
}
};
}




UI는 잘 나오는데 버튼이 동작을 안한다는 말씀이신거죠??
mStartListener를 선언만 하고,
어떤 버튼에서도 mStartListener를 등록하지 않았내요.
startbutton.setOnClickListener(mStartListener);
...
...
이런식으로 3개의 버튼을 전부 등록해보세요