Ich baue eine einfache Timer-App, und ich möchte es editierbar machen, so dass der Benutzer Stunde, Minuten, Sekunden wählen kann. Ich habe versucht, EditText zu verwenden, aber es funktioniert nicht wirklich, es lässt mich wirklich nichts ändern. Dies ist der Code:Erstellen Sie einen Timer, den Sie bearbeiten können Android (Java)
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_stopwatch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.adir.stopwatch.StopwatchActivity"
android:background="#000000"
>
<Button
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickReset"
android:text="@string/reset"
android:textColor="#FFFFFF"
android:layout_below="@+id/stop_button"
android:layout_alignLeft="@+id/stop_button"
android:layout_alignStart="@+id/stop_button" />
<EditText
android:id="@+id/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="92sp"
android:textColor="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickStart"
android:text="@string/start"
android:textColor="#FFFFFF"
android:layout_below="@+id/time_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickStop"
android:text="@string/stop"
android:textColor="#FFFFFF"
android:layout_alignLeft="@+id/start_button"
android:layout_alignStart="@+id/start_button"
android:layout_below="@+id/start_button"></Button>
</RelativeLayout>
Java-Code:
public class StopwatchActivity extends Activity {
private int seconds=0;
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
runTimer();
}
public void onClickStart(View view) {
running = true; }
public void onClickStop(View view) {
running = false;
}
public void onClickReset(View view) {
running = false;
seconds = 0;
}
private void runTimer() {
final EditText timeView = (EditText) findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours=seconds/3600;
int minutes=(seconds%3600)/60;
int secs=seconds%60;
String time = String.format("%d:%02d:%02d",hours, minutes, secs);
timeView.setText(time);
if (running) {
seconds--;
}
handler.postDelayed(this, 1000);
}
});
}
}
Wie kann ich es beheben?
Verwenden Sie einen EditText oder TimePicker – Gumbo
@Gumbo Ich habe versucht, EditText zu verwenden, aber es hat nicht wirklich funktioniert es zeigte nur, dass es bearbeitbar ist, gab aber keine Option, Dinge zu ändern .. – adir
Die Sache mit StackOverflow ist, dass es ist nur für bestimmte Fragen gedacht, die das Gespräch objektiv halten. Wenn Sie nur fragen "Wie kann ich xyz machen", wäre die Antwort entweder Seiten lang, weil es normalerweise Dutzende Möglichkeiten gibt, xyz zu tun, oder nicht wirklich hilfreich. Wenn Sie also ein sehr spezifisches Problem haben, EditText zu benutzen, können Sie es hier fragen, aber diese Frage entspricht nicht den Regeln und somit werden Sie von Leuten abgelehnt. Siehe [Hilfe] für weitere Informationen. – Gumbo