4

我有 100 个按钮的布局和OnClick()方法。

如果我使用switch我需要case R.id.button1, ..., case R.id.button100为所有 100 个按钮做。如何缩短此代码?

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

6 回答 6

5

如果 URL 直接依赖于 ID,那么试试这个:

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

已编辑

如果您的 URL 不直接依赖于 ID,请尝试使用 URLS 映射按钮 ID,如下所示:

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

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

并像这样修改上面的代码:

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

编辑#2

如果在按钮的标签中已经有了 URL,那么建议(不是我的,而是由 @pad 提出的)将使用它来以这种方式计算 URL:

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);
}
于 2013-08-05T10:55:56.767 回答
2

对于您的情况下更多数量的按钮,在循环中动态创建按钮并分配 onclick 事件,如..

LinearLayout在您的 xml 文件中创建一个。

现在将所有按钮添加到该 LinearLayout 中,例如..

String[] urls={url1,url2.....url100}; //  here write your all URLs
Button button[]= new Button[100];
LinearLayout mainlinear=(LinearLayout) findViewById(R.id.main_layer);
for (int i = 0; i < 100; i++) {
            button[i] = new Button(this);
            button[i].setText(i);
            button[i].setTag(i+":"+URL); // URL = What you need to pass when button is click
            button[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                                    TextView selected = (TextView) v;
                                    String tag = selected.getTag().toString();

            //Here you need to write your code when button is pressed.

Intent intent = new Intent(this, Webview.class);
        intent2.putExtra("weblink",urls[i]);
        startActivity(intent2);
            }

            }
    mainlinear.addView(button[i]);
于 2013-08-05T11:00:44.017 回答
0

我认为这应该有效:

public void webClick(View v) 
            {
                  String stringID = String.valueOf(v.getId());
                  String[] temp = stringID.split("utton");
                  stringID = "file:///android_asset/chapter/chapter" +temp[1]+ ".html"
                  Intent intent = new Intent(this, Webview.class);
                  intent.putExtra("weblink",stringID);
                  startActivity(intent);
            }
于 2013-08-05T11:01:57.230 回答
0

您可能想要使用 ListView。如果您想要按钮,请使用 ListView,它在每个项目的布局中都有一个按钮。

然后,您设置一个 onItemClickListener。您将获得该按钮的位置(第一个的位置为 0,第二个的位置为 1,等等)。然后编写一个链接:

String link= "file:///android_asset/chapter/chapter" + (position +1) + ".html";

您的其余代码将是相同的 ( intent.putExtra("weblink", link);)

于 2013-08-05T11:02:18.617 回答
0

我的建议是在这种情况下使用 ListView 或 GridView 并将文件名保存在 ArrayList 中。然后你做这样的事情:

List<String> urlList = new ArrayList<String>();
urlList.add("file:///android_asset/chapter/chapter1.html");
urlList.add("file:///android_asset/chapter/chapter2.html");
// and so on

ListView listView = (ListView)findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.id.simple_list_item_1, urlList);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                    Intent intent = new Intent(this, Webview.class);
                    intent.putExtra("weblink", urlList.get(position));
                    startActivity(intent);

            }
        });

这将是一个很好的实现,因为您将避免使用 100 个按钮和 OnClickListeners 来保存一些资源。

编辑:此代码应该正在运行,只需将其放入 onCreate() 进行一些测试。您所要做的就是在您的 layout.xml 中定义一个 ListView,而不是像这样的 100 个按钮:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent">  

    <ListView android:layout_width="match_parent"   
      android:layout_height="match_parent"   
      android:id="@+id/listView">  
    </ListView>  

</LinearLayout>

如果您真的想将其与 Buttons 一起使用,请遵循 morgano 的建议。

于 2013-08-05T11:04:13.157 回答
-5
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;
     }

}
于 2013-08-05T10:55:01.870 回答