gpt4 book ai didi

java - 安卓 : Making simple code. 干

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:21 27 4
gpt4 key购买 nike

我是 Java 编程的新手。这里有人可以帮我让我的代码保持干燥吗?

Button level01 = (Button) findViewById(R.id.level01);
level01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Prefs.setStagePref(getApplicationContext(), 1);
Intent play = new Intent(LevelActivity.this, PlayActivity.class);
startActivity(play);
}
});

Button level02 = (Button) findViewById(R.id.level02);
level02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Prefs.setStagePref(getApplicationContext(), 2);
Intent play = new Intent(LevelActivity.this, PlayActivity.class);
startActivity(play);
}
});

Button level03 = (Button) findViewById(R.id.level03);
level03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Prefs.setStagePref(getApplicationContext(), 3);
Intent play = new Intent(LevelActivity.this, PlayActivity.class);
startActivity(play);
}
});

我想制作超过 20 个按钮,那么如何让这段代码变干。

谢谢

最佳答案

把你的重复功能放在方法中

private void f(int i){
Prefs.setStagePref(getApplicationContext(), i);
Intent play = new Intent(LevelActivity.this, PlayActivity.class);
startActivity(play);
}

然后改变你处理点击的方式

在“onClick”布局中

   <ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/saveButton" android:src="@android:drawable/ic_menu_save"
android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/wallpaperButton" android:src="@android:drawable/ic_menu_gallery"
android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/shareButton" android:src="@android:drawable/ic_menu_share"
android:layout_weight="1" android:background="@drawable/menu_button" android:onClick="onClick"
android:focusableInTouchMode="false"/>

Activity 中

public void onClick(View cview) throws IOException {
switch (cview.getId()) {
case R.id.saveButton:
f(1);
break;

case R.id.shareButton:
f(2);
break;

case R.id.wallpaperButton:
f(3);
break;
}
}

MainActivity implements OnClickListener

...

Button level1 = (Button) findViewById(R.id.level1);
level1.setOnClickListener(this);
Button level2 = (Button) findViewById(R.id.level2);
level2.setOnClickListener(this);
Button level3 = (Button) findViewById(R.id.level3);
level3.setOnClickListener(this);
...

public void onClick(View cview) throws IOException {
switch (cview.getId()) {
case R.id.level1:
f(1);
break;

case R.id.level2:
f(2);
break;

case R.id.level3:
f(3);
break;
}
}

关于java - 安卓 : Making simple code. 干,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32677415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com