用户是否可以在android studio中的活动中添加按钮
所以我正在做我的大学项目,还是一个android的新手。我想用七个按钮来表示用户上班的天数(因为一周7天) 因为我们中的许多人并不是一周工作七天,所以我希望用户选择并创建一周中的几天。 假设他星期一到星期五上班,他应该可以加5个按钮 搜索了谷歌和安卓的网站,但没有找到答案。用户是否可以在android studio中的活动中添加按钮,android,android-studio,user-controls,android-button,Android,Android Studio,User Controls,Android Button,所以我正在做我的大学项目,还是一个android的新手。我想用七个按钮来表示用户上班的天数(因为一周7天) 因为我们中的许多人并不是一周工作七天,所以我希望用户选择并创建一周中的几天。 假设他星期一到星期五上班,他应该可以加5个按钮 搜索了谷歌和安卓的网站,但没有找到答案。 请提供一些解释,因为这将有助于像我这样的初学者。谢谢使用所有7个按钮设计布局,但根据用户驱动的条件,使用buttonMonday.setVisibility(View.GONE)从布局中隐藏相关按钮View.GONE将从布局
请提供一些解释,因为这将有助于像我这样的初学者。谢谢使用所有7个按钮设计布局,但根据用户驱动的条件,使用
buttonMonday.setVisibility(View.GONE)从布局中隐藏相关按钮代码>View.GONE
将从布局中删除视图,而View.INVISIBLE
将隐藏视图。这不是唯一的方法,但您可以尝试以下方法:
活动\u 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>
所以,如果我做对了,我应该设置编辑按钮或类似的设置,用户可以选中或取消选中所有7个按钮。正确。这可能是您将设置存储在共享首选项或数据库中的一次性设置,或者您可能需要询问用户每次使用该应用程序的情况。另外,该解决方案似乎很有用,但我不知道它将如何运行。只是想问一下,如果我有一个问题,最大按钮的数量是不固定的,有没有任何解决方案,用户可以添加他选择的切换按钮的数量,除了我使如果可见或不可见?比如,如果他去工作,切换将为真,否则为假,如果你有一个固定的最大值,使用我的方法。如果你真的需要更多的灵活性,请看,但显然这需要做更多的工作。@RyanM是的,我想这是我想要的,但从来没有用这些关键字在谷歌上搜索过谢谢
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);
}