如何在100手柄按钮的onClick()? [英] How to handle onClick() in 100 buttons?

查看:107
本文介绍了如何在100手柄按钮的onClick()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在布局100按钮和的OnClick()为它的所有方法。

如果我用开关我需要做的情况下R.id.button1,...,情况R.id.button100 所有100的按钮。如何缩短这个code?

 公共无效webClick(视图V)
{
    开关(v.getId())
    {
    案例R.id.button1:
        意向意图=新意图(这一点,Webview.class);
        intent.putExtra(网络链接,文件:///android_asset/chapter/chapter1.html);
        startActivity(意向);
        打破;    案例R.id.button2:
        意图int​​ent2 =新意图(这一点,Webview.class);
        intent2.putExtra(网络链接,文件:///android_asset/chapter/chapter2.html);
        startActivity(intent2);
        打破;    // ...    案例R.id.button100:
        意图int​​ent100 =新意图(这一点,Webview.class);
        intent100.putExtra(网络链接,文件:///android_asset/chapter/chapter100.html);
        startActivity(intent100);
        打破;
    }
 }


解决方案

如果该URL直接取决于ID,那么试试这个:

 公共无效webClick(视图V)
{
    意向意图=新意图(这一点,Webview.class);
    intent.putExtra(网络链接,文件:/// android_asset /章节/章节+ v.getId()+。html的);
    startActivity(意向);
}

EDITED

在情况下,你的网址不直接上的ID依赖,然后尝试使用URL映射按钮标识,如:

 地图<整数,字符串>网址=新的HashMap();urls.put(R.id.button1,文件:///android_asset/chapter/chapter100.html);
// ... 1到100 ...

和修改上面的code是这样的:

 公共无效webClick(视图V)
{
    意向意图=新意图(这一点,Webview.class);
    intent.putExtra(网络链接,urls.get(v.getId()));
    startActivity(意向);
}

EDITED#2

如果您在按钮的标签你已经拥有的网址,一个sugestion(不是我而是由@pad制造)将用它来计算的网址是这样的:

 公共无效webClick(视图V)
{
    意向意图=新意图(这一点,Webview.class);
    intent.putExtra(网络链接,文件:/// android_asset /章节/章节+ v.getText()的replaceAll(章,)+。html的); //假设你的文字就像是50章
    startActivity(意向);
}

I have 100 buttons in layout and OnClick() method for all of it.

If I use switch I need to do case R.id.button1, ..., case R.id.button100 for all 100 buttons. How to shorten this code?

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;
    }
 }

解决方案

If the URL depends directly on the ID, then try this:

public void webClick(View v) 
{
    Intent intent = new Intent(this, Webview.class);
    intent.putExtra("weblink","file:///android_asset/chapter/chapter" + v.getId() + ".html");
    startActivity(intent);
}

EDITED

In the case you URL doesn't depend directly on the ID, then try mapping button IDs with URLS, like this:

Map<Integer, String> urls = new HashMap();

urls.put(R.id.button1, "file:///android_asset/chapter/chapter100.html");
// ... 1 to 100 ...

and modify the above code like this:

public void webClick(View v) 
{
    Intent intent = new Intent(this, Webview.class);
    intent.putExtra("weblink", urls.get(v.getId()));
    startActivity(intent);
}

EDITED #2

If in the label of your buttons you already have the URL, a sugestion (not mine but made by @pad) would be to use it to calculate the URL this way:

public void webClick(View v) 
{
    Intent intent = new Intent(this, Webview.class);
    intent.putExtra("weblink", "file:///android_asset/chapter/chapter" + v.getText().replaceAll("Chapter ","") + ".html"); // Assuming your text is like "Chapter 50"
    startActivity(intent);
}

这篇关于如何在100手柄按钮的onClick()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆