首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何处理100个按钮中的onClick()?

如何处理100个按钮中的onClick()?
EN

Stack Overflow用户
提问于 2013-08-05 18:49:52
回答 6查看 218关注 0票数 4

我在layout和OnClick()方法中有100个按钮。

如果我使用switch,我需要对所有100个按钮执行case R.id.button1, ..., case R.id.button100。如何缩短这段代码?

代码语言:javascript
复制
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;
    }
 }
EN

Stack Overflow用户

发布于 2013-08-05 18:55:02

代码语言:javascript
复制
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;
     }

}
票数 -5
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18056367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档