有什么方法可以让用户在 android studio 中的 activity 中添加按钮
Is there any way that the user can add button in an activity in android studio
所以我正在研究我的大学项目,并且在 android 中仍然是一个新手。我想用七个按钮表示用户上班的天数(7 是因为一周有 7 天)。
由于我们中的许多人并非一周工作 7 天,我希望用户选择并创建一周中的几天来表示按钮。
假设如果他周一至周五工作,他应该能够添加 5 个按钮。
搜索了 google 和 android 的站点,但无法解决这个问题。
请提供一些解释,因为这对像我这样的初学者会有帮助。谢谢
使用所有 7 个按钮设计您的布局,但根据用户驱动的条件,使用 buttonMonday.setVisibility(View.GONE);
从布局中隐藏相关按钮。 View.GONE
将从布局中删除视图,而 View.INVISIBLE
只会隐藏它。
这不是唯一的方法,但也许你可以试试这个:
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CheckBox
android:id="@+id/sunday"
android:layout_width="120dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_height="wrap_content"
android:text="Sunday"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<CheckBox
android:id="@+id/monday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Monday"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/tuesday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Tuesday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sunday" />
<CheckBox
android:id="@+id/wednesday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Wednesday"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/monday" />
<CheckBox
android:id="@+id/thrusday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Thrusday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tuesday" />
<CheckBox
android:id="@+id/friday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Friday"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tuesday" />
<CheckBox
android:id="@+id/saturday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Saturday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thrusday" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/parent">
<Button
android:id="@+id/btnSunday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnMonday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Monday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSunday" />
<Button
android:id="@+id/btnTuesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tuesday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnMonday" />
<Button
android:id="@+id/btnWednesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wednesday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTuesday" />
<Button
android:id="@+id/btnThrusday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thrusday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnWednesday" />
<Button
android:id="@+id/btnFriday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnThrusday" />
<Button
android:id="@+id/btnSaturday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saturday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnFriday" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
CheckBox sunday, monday, tuesday, wednesday, thrusday, friday, saturday;
Button btnSunday, btnMonday, btnTuesday, btnWednesday, btnThrusday, btnFriday, btnSaturday, open;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
init();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("sunday") && preferences.getBoolean("sunday",false) == true) {
sunday.setChecked(true);
btnSunday.setVisibility(View.VISIBLE);
}else {
sunday.setChecked(false);
btnSunday.setVisibility(View.GONE);
}
if(preferences.contains("monday") && preferences.getBoolean("monday",false) == true) {
monday.setChecked(true);
btnMonday.setVisibility(View.VISIBLE);
}else {
monday.setChecked(false);
btnMonday.setVisibility(View.GONE);
}
if(preferences.contains("tuesday") && preferences.getBoolean("tuesday",false) == true) {
tuesday.setChecked(true);
btnTuesday.setVisibility(View.VISIBLE);
}else {
tuesday.setChecked(false);
btnTuesday.setVisibility(View.GONE);
}
if(preferences.contains("wednesday") && preferences.getBoolean("wednesday",false) == true) {
wednesday.setChecked(true);
btnWednesday.setVisibility(View.VISIBLE);
}else {
wednesday.setChecked(false);
btnWednesday.setVisibility(View.GONE);
}
if(preferences.contains("thrusday") && preferences.getBoolean("thrusday",false) == true) {
thrusday.setChecked(true);
btnThrusday.setVisibility(View.VISIBLE);
}else {
thrusday.setChecked(false);
btnThrusday.setVisibility(View.GONE);
}
if(preferences.contains("friday") && preferences.getBoolean("friday",false) == true) {
friday.setChecked(true);
btnFriday.setVisibility(View.VISIBLE);
}else {
friday.setChecked(false);
btnFriday.setVisibility(View.GONE);
}
if(preferences.contains("saturday") && preferences.getBoolean("saturday",false) == true) {
saturday.setChecked(true);
btnSaturday.setVisibility(View.VISIBLE);
}else {
saturday.setChecked(false);
btnSaturday.setVisibility(View.GONE);
}
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sunday.isChecked()){
btnSunday.setVisibility(View.VISIBLE);
editor.putBoolean("sunday", true);
editor.apply();
} else {
btnSunday.setVisibility(View.GONE);
editor.putBoolean("sunday", false);
editor.apply();
}
if (monday.isChecked()){
btnMonday.setVisibility(View.VISIBLE);
editor.putBoolean("monday", true);
editor.apply();
} else {
btnMonday.setVisibility(View.GONE);
editor.putBoolean("monday", false);
editor.apply();
}
if (tuesday.isChecked()){
btnTuesday.setVisibility(View.VISIBLE);
editor.putBoolean("tuesday", true);
editor.apply();
} else {
btnTuesday.setVisibility(View.GONE);
editor.putBoolean("tuesday", false);
editor.apply();
}
if (wednesday.isChecked()){
btnWednesday.setVisibility(View.VISIBLE);
editor.putBoolean("wednesday", true);
editor.apply();
} else {
btnWednesday.setVisibility(View.GONE);
editor.putBoolean("wednesday", false);
editor.apply();
}
if (thrusday.isChecked()){
btnThrusday.setVisibility(View.VISIBLE);
editor.putBoolean("thrusday", true);
editor.apply();
} else {
btnThrusday.setVisibility(View.GONE);
editor.putBoolean("thrusday", false);
editor.apply();
}
if (friday.isChecked()){
btnFriday.setVisibility(View.VISIBLE);
editor.putBoolean("friday", true);
editor.apply();
} else {
btnFriday.setVisibility(View.GONE);
editor.putBoolean("friday", false);
editor.apply();
}
if (saturday.isChecked()){
btnSaturday.setVisibility(View.VISIBLE);
editor.putBoolean("saturday", true);
editor.apply();
} else {
btnSaturday.setVisibility(View.GONE);
editor.putBoolean("saturday", false);
editor.apply();
}
}
});
}
public void init(){
sunday = findViewById(R.id.sunday);
monday = findViewById(R.id.monday);
tuesday = findViewById(R.id.tuesday);
wednesday = findViewById(R.id.wednesday);
thrusday = findViewById(R.id.thrusday);
friday = findViewById(R.id.friday);
saturday = findViewById(R.id.saturday);
btnSunday = findViewById(R.id.btnSunday);
btnMonday = findViewById(R.id.btnMonday);
btnTuesday = findViewById(R.id.btnTuesday);
btnWednesday = findViewById(R.id.btnWednesday);
btnThrusday = findViewById(R.id.btnThrusday);
btnFriday = findViewById(R.id.btnFriday);
btnSaturday = findViewById(R.id.btnSaturday);
open = findViewById(R.id.open);
}
所以我正在研究我的大学项目,并且在 android 中仍然是一个新手。我想用七个按钮表示用户上班的天数(7 是因为一周有 7 天)。
由于我们中的许多人并非一周工作 7 天,我希望用户选择并创建一周中的几天来表示按钮。 假设如果他周一至周五工作,他应该能够添加 5 个按钮。
搜索了 google 和 android 的站点,但无法解决这个问题。 请提供一些解释,因为这对像我这样的初学者会有帮助。谢谢
使用所有 7 个按钮设计您的布局,但根据用户驱动的条件,使用 buttonMonday.setVisibility(View.GONE);
从布局中隐藏相关按钮。 View.GONE
将从布局中删除视图,而 View.INVISIBLE
只会隐藏它。
这不是唯一的方法,但也许你可以试试这个:
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CheckBox
android:id="@+id/sunday"
android:layout_width="120dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_height="wrap_content"
android:text="Sunday"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<CheckBox
android:id="@+id/monday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Monday"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/tuesday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Tuesday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sunday" />
<CheckBox
android:id="@+id/wednesday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Wednesday"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/monday" />
<CheckBox
android:id="@+id/thrusday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Thrusday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tuesday" />
<CheckBox
android:id="@+id/friday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Friday"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tuesday" />
<CheckBox
android:id="@+id/saturday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Saturday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thrusday" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/parent">
<Button
android:id="@+id/btnSunday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnMonday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Monday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSunday" />
<Button
android:id="@+id/btnTuesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tuesday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnMonday" />
<Button
android:id="@+id/btnWednesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wednesday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTuesday" />
<Button
android:id="@+id/btnThrusday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thrusday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnWednesday" />
<Button
android:id="@+id/btnFriday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnThrusday" />
<Button
android:id="@+id/btnSaturday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saturday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnFriday" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
CheckBox sunday, monday, tuesday, wednesday, thrusday, friday, saturday;
Button btnSunday, btnMonday, btnTuesday, btnWednesday, btnThrusday, btnFriday, btnSaturday, open;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
init();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("sunday") && preferences.getBoolean("sunday",false) == true) {
sunday.setChecked(true);
btnSunday.setVisibility(View.VISIBLE);
}else {
sunday.setChecked(false);
btnSunday.setVisibility(View.GONE);
}
if(preferences.contains("monday") && preferences.getBoolean("monday",false) == true) {
monday.setChecked(true);
btnMonday.setVisibility(View.VISIBLE);
}else {
monday.setChecked(false);
btnMonday.setVisibility(View.GONE);
}
if(preferences.contains("tuesday") && preferences.getBoolean("tuesday",false) == true) {
tuesday.setChecked(true);
btnTuesday.setVisibility(View.VISIBLE);
}else {
tuesday.setChecked(false);
btnTuesday.setVisibility(View.GONE);
}
if(preferences.contains("wednesday") && preferences.getBoolean("wednesday",false) == true) {
wednesday.setChecked(true);
btnWednesday.setVisibility(View.VISIBLE);
}else {
wednesday.setChecked(false);
btnWednesday.setVisibility(View.GONE);
}
if(preferences.contains("thrusday") && preferences.getBoolean("thrusday",false) == true) {
thrusday.setChecked(true);
btnThrusday.setVisibility(View.VISIBLE);
}else {
thrusday.setChecked(false);
btnThrusday.setVisibility(View.GONE);
}
if(preferences.contains("friday") && preferences.getBoolean("friday",false) == true) {
friday.setChecked(true);
btnFriday.setVisibility(View.VISIBLE);
}else {
friday.setChecked(false);
btnFriday.setVisibility(View.GONE);
}
if(preferences.contains("saturday") && preferences.getBoolean("saturday",false) == true) {
saturday.setChecked(true);
btnSaturday.setVisibility(View.VISIBLE);
}else {
saturday.setChecked(false);
btnSaturday.setVisibility(View.GONE);
}
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sunday.isChecked()){
btnSunday.setVisibility(View.VISIBLE);
editor.putBoolean("sunday", true);
editor.apply();
} else {
btnSunday.setVisibility(View.GONE);
editor.putBoolean("sunday", false);
editor.apply();
}
if (monday.isChecked()){
btnMonday.setVisibility(View.VISIBLE);
editor.putBoolean("monday", true);
editor.apply();
} else {
btnMonday.setVisibility(View.GONE);
editor.putBoolean("monday", false);
editor.apply();
}
if (tuesday.isChecked()){
btnTuesday.setVisibility(View.VISIBLE);
editor.putBoolean("tuesday", true);
editor.apply();
} else {
btnTuesday.setVisibility(View.GONE);
editor.putBoolean("tuesday", false);
editor.apply();
}
if (wednesday.isChecked()){
btnWednesday.setVisibility(View.VISIBLE);
editor.putBoolean("wednesday", true);
editor.apply();
} else {
btnWednesday.setVisibility(View.GONE);
editor.putBoolean("wednesday", false);
editor.apply();
}
if (thrusday.isChecked()){
btnThrusday.setVisibility(View.VISIBLE);
editor.putBoolean("thrusday", true);
editor.apply();
} else {
btnThrusday.setVisibility(View.GONE);
editor.putBoolean("thrusday", false);
editor.apply();
}
if (friday.isChecked()){
btnFriday.setVisibility(View.VISIBLE);
editor.putBoolean("friday", true);
editor.apply();
} else {
btnFriday.setVisibility(View.GONE);
editor.putBoolean("friday", false);
editor.apply();
}
if (saturday.isChecked()){
btnSaturday.setVisibility(View.VISIBLE);
editor.putBoolean("saturday", true);
editor.apply();
} else {
btnSaturday.setVisibility(View.GONE);
editor.putBoolean("saturday", false);
editor.apply();
}
}
});
}
public void init(){
sunday = findViewById(R.id.sunday);
monday = findViewById(R.id.monday);
tuesday = findViewById(R.id.tuesday);
wednesday = findViewById(R.id.wednesday);
thrusday = findViewById(R.id.thrusday);
friday = findViewById(R.id.friday);
saturday = findViewById(R.id.saturday);
btnSunday = findViewById(R.id.btnSunday);
btnMonday = findViewById(R.id.btnMonday);
btnTuesday = findViewById(R.id.btnTuesday);
btnWednesday = findViewById(R.id.btnWednesday);
btnThrusday = findViewById(R.id.btnThrusday);
btnFriday = findViewById(R.id.btnFriday);
btnSaturday = findViewById(R.id.btnSaturday);
open = findViewById(R.id.open);
}