我在layout和OnClick()方法中有100个按钮。
如果我使用switch,我需要对所有100个按钮执行case R.id.button1, ..., case R.id.button100。如何缩短这段代码?
public void webClick(View v)
{
switch(v.getId())
{
case R.id.button1:
Intent intent = new Intent(this, Webview.class);
intent.putExtra("weblink","file:///android_asset/chapter/chapter1.html");
startActivity(intent);
break;
case R.id.button2:
Intent intent2 = new Intent(this, Webview.class);
intent2.putExtra("weblink","file:///android_asset/chapter/chapter2.html");
startActivity(intent2);
break;
// ...
case R.id.button100:
Intent intent100 = new Intent(this, Webview.class);
intent100.putExtra("weblink","file:///android_asset/chapter/chapter100.html");
startActivity(intent100);
break;
}
}发布于 2013-08-05 18:55:02
public void webClick(View v)
{
Intent intent = new Intent(this, Webview.class);
switch(v.getId())
{
case R.id.button1:
intent.putExtra("weblink","file:///android_asset/chapter/chapter1.html");
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("weblink","file:///android_asset/chapter/chapter2.html");
startActivity(intent);
break;
case R.id.button3:
intent.putExtra("weblink","file:///android_asset/chapter/chapter3.html");
startActivity(intent);
break;
.
.
.
.
case R.id.button100:
intent.putExtra("weblink","file:///android_asset/chapter/chapter100.html");
startActivity(intent);
break;
default:
break;
}
}https://stackoverflow.com/questions/18056367
复制相似问题