在 Android Studio 中编辑文本和倒数计时器
Edit Text And Count Down Timer in Android Studio
我的应用程序上有一个 Counttimer 和 edittext。我想在edittext中写一个数字,当我点击按钮时,倒计时就会开始,时间是edittext变量。在 XML 中,我有一个示例编号,因为当我删除它时,它就不起作用了。当我启动倒数计时器时,它会工作,但如果我更改 edittext,则它会使用旧的 edittext 数据。我该如何解决我的问题?
pomodoro.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pomodoro"
android:background="@drawable/backround_home">
<
<EditText
android:id="@+id/edittextcalisma"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="20"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="25dp"
android:background="@drawable/edittext"
android:layout_marginLeft="30dp"
android:visibility="visible"
android:inputType="number"
android:maxLength="2"
android:lines="1"
/>
<EditText
android:id="@+id/edittextmola"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="5"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="25dp"
android:background="@drawable/edittext"
android:visibility="visible"
android:layout_marginLeft="30dp"
android:inputType="number"
android:maxLength="2"
android:lines="1"
/>
<Button
android:id="@+id/baslatbutton"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Başla"
android:textSize="25dp"
android:layout_marginTop="400dp"
android:textColor="#ffffff"
android:fontFamily="@font/montserrat_regular"
android:background="@drawable/button"
android:layout_centerHorizontal="true"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:layout_centerInParent="true"
android:fontFamily="@font/montserrat_regular"
android:textColor="#000000"
android:id="@+id/pomodoro"
android:visibility="invisible"
/>
</RelativeLayout>
Java.class
public class pomodoro extends AppCompatActivity {
Button baslat;
EditText edittextcalisma,edittextmola;
TextView textcalisma,textmola,pomodoro;
CountDownTimer calisma,mola;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pomodoro);
baslat = findViewById(R.id.baslatbutton);
edittextcalisma = findViewById(R.id.edittextcalisma);
edittextmola = findViewById(R.id.edittextmola);
textcalisma = findViewById(R.id.textcalisma);
textmola = findViewById(R.id.textmola);
String timercalisma = edittextcalisma.getText().toString();
String timermola = edittextmola.getText().toString();
long calismapo = Long.parseLong(timercalisma) * 60000;
long molapo = Long.parseLong(timermola) * 60000;
pomodoro = findViewById(R.id.pomodoro);
baslat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pomodoro.setVisibility(View.VISIBLE);
calisma.start();
}
});
calisma = new CountDownTimer(calismapo, 1000) {
@Override
public void onTick(long millis) {
int minuted1 = (int) (millis / 1000) / 60;
int secondd1 = (int) (millis / 1000) % 60;
pomodoro.setText(minuted1 + ":" + secondd1);
if (secondd1 < 10) {
pomodoro.setText(minuted1 + ":0" + secondd1);
}
}
@Override
public void onFinish() {
calisma.cancel();
mola.start();
}
};
mola = new CountDownTimer(molapo, 1000) {
@Override
public void onTick(long molapo) {
int minuted1 = (int) (molapo / 1000) / 60;
int secondd1 = (int) (molapo / 1000) % 60;
pomodoro.setText(minuted1 + ":" + secondd1);
if (secondd1 < 10) {
pomodoro.setText(minuted1 + ":0" + secondd1);
}
}
@Override
public void onFinish() {
mola.cancel();
pomodoro.setText("Bitti");
}
};
}
}
and the time is edittext variable. In XML, I have got an example
number, because when I delete it, then it's not working.
因为您在 onCreate 中读取了 EditText 字段并且您的代码需要一个数字,所以这就是为什么您需要在 XML 中设置“示例”数字的原因。
当您从 XML 中删除号码时,您需要:
检查空值
String timercalisma = edittextcalisma.getText().toString();
String timermola = edittextmola.getText().toString();
if(timercalisma == null || timercalisma.isEmpty()) {
timercalisma = "0";
}
if(timermola == null || timermola.isEmpty()) {
timermola = "0";
}
long calismapo = Long.parseLong(timercalisma) * 60000;
long molapo = Long.parseLong(timermola) * 60000;
或阅读按下按钮时的字段
baslat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String timercalisma = edittextcalisma.getText().toString();
String timermola = edittextmola.getText().toString();
long calismapo = Long.parseLong(timercalisma) * 60000;
long molapo = Long.parseLong(timermola) * 60000;
pomodoro.setVisibility(View.VISIBLE);
calisma.start();
}
});
我的应用程序上有一个 Counttimer 和 edittext。我想在edittext中写一个数字,当我点击按钮时,倒计时就会开始,时间是edittext变量。在 XML 中,我有一个示例编号,因为当我删除它时,它就不起作用了。当我启动倒数计时器时,它会工作,但如果我更改 edittext,则它会使用旧的 edittext 数据。我该如何解决我的问题?
pomodoro.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pomodoro"
android:background="@drawable/backround_home">
<
<EditText
android:id="@+id/edittextcalisma"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="20"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="25dp"
android:background="@drawable/edittext"
android:layout_marginLeft="30dp"
android:visibility="visible"
android:inputType="number"
android:maxLength="2"
android:lines="1"
/>
<EditText
android:id="@+id/edittextmola"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="5"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="25dp"
android:background="@drawable/edittext"
android:visibility="visible"
android:layout_marginLeft="30dp"
android:inputType="number"
android:maxLength="2"
android:lines="1"
/>
<Button
android:id="@+id/baslatbutton"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Başla"
android:textSize="25dp"
android:layout_marginTop="400dp"
android:textColor="#ffffff"
android:fontFamily="@font/montserrat_regular"
android:background="@drawable/button"
android:layout_centerHorizontal="true"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:layout_centerInParent="true"
android:fontFamily="@font/montserrat_regular"
android:textColor="#000000"
android:id="@+id/pomodoro"
android:visibility="invisible"
/>
</RelativeLayout>
Java.class
public class pomodoro extends AppCompatActivity {
Button baslat;
EditText edittextcalisma,edittextmola;
TextView textcalisma,textmola,pomodoro;
CountDownTimer calisma,mola;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pomodoro);
baslat = findViewById(R.id.baslatbutton);
edittextcalisma = findViewById(R.id.edittextcalisma);
edittextmola = findViewById(R.id.edittextmola);
textcalisma = findViewById(R.id.textcalisma);
textmola = findViewById(R.id.textmola);
String timercalisma = edittextcalisma.getText().toString();
String timermola = edittextmola.getText().toString();
long calismapo = Long.parseLong(timercalisma) * 60000;
long molapo = Long.parseLong(timermola) * 60000;
pomodoro = findViewById(R.id.pomodoro);
baslat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pomodoro.setVisibility(View.VISIBLE);
calisma.start();
}
});
calisma = new CountDownTimer(calismapo, 1000) {
@Override
public void onTick(long millis) {
int minuted1 = (int) (millis / 1000) / 60;
int secondd1 = (int) (millis / 1000) % 60;
pomodoro.setText(minuted1 + ":" + secondd1);
if (secondd1 < 10) {
pomodoro.setText(minuted1 + ":0" + secondd1);
}
}
@Override
public void onFinish() {
calisma.cancel();
mola.start();
}
};
mola = new CountDownTimer(molapo, 1000) {
@Override
public void onTick(long molapo) {
int minuted1 = (int) (molapo / 1000) / 60;
int secondd1 = (int) (molapo / 1000) % 60;
pomodoro.setText(minuted1 + ":" + secondd1);
if (secondd1 < 10) {
pomodoro.setText(minuted1 + ":0" + secondd1);
}
}
@Override
public void onFinish() {
mola.cancel();
pomodoro.setText("Bitti");
}
};
}
}
and the time is edittext variable. In XML, I have got an example number, because when I delete it, then it's not working.
因为您在 onCreate 中读取了 EditText 字段并且您的代码需要一个数字,所以这就是为什么您需要在 XML 中设置“示例”数字的原因。
当您从 XML 中删除号码时,您需要:
检查空值
String timercalisma = edittextcalisma.getText().toString();
String timermola = edittextmola.getText().toString();
if(timercalisma == null || timercalisma.isEmpty()) {
timercalisma = "0";
}
if(timermola == null || timermola.isEmpty()) {
timermola = "0";
}
long calismapo = Long.parseLong(timercalisma) * 60000;
long molapo = Long.parseLong(timermola) * 60000;
或阅读按下按钮时的字段
baslat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String timercalisma = edittextcalisma.getText().toString();
String timermola = edittextmola.getText().toString();
long calismapo = Long.parseLong(timercalisma) * 60000;
long molapo = Long.parseLong(timermola) * 60000;
pomodoro.setVisibility(View.VISIBLE);
calisma.start();
}
});