Java 什么';开关怎么了?

Java 什么';开关怎么了?,java,android,Java,Android,我有一个函数: public void vfShareQuote (String textToShare){ Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare); sendIntent.setType("text/plain"); startActivit

我有一个函数:

public void vfShareQuote (String textToShare){
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
}
还有许多以编程方式创建的按钮,其中有两个:

Button agafon_1 = new Button(this);agafon_1.setText(R.string.agafon_1);llPreViewList.addView(agafon_1, lParams);
Button agafon_2 = new Button(this);agafon_2.setText(R.string.agafon_2);llPreViewList.addView(agafon_2, lParams);
以下是OnClickListener:

OnClickListener oclShareQuote = new OnClickListener() {
    @Override
    public void onClick(View v) {
//Set the text based on the selected button and send it to function vfShareQuote
    switch (v.getId()) {
    case R.string.agafon_1:
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_1));
        break;
    case R.string.agafon_2:
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_2));
        break;
    }
    }
};
当然:

agafon_1.setOnClickListener(oclShareQuote);
agafon_2.setOnClickListener(oclShareQuote);
但是当你按下按钮时,什么也没有发生。为什么?还是以编程方式创建按钮? 怎么办?
由google翻译。

因为
R.string.agafon_1
R.string.agafon_2
不是id。它们只是字符串资源的id。将id设置为按钮并使用它们。像

agafon_1.setId(id1);
agafon_2.setId(id2);
其中,id1和id2是两个int。并使用它们

OnClickListener oclShareQuote = new OnClickListener() {
    @Override
    public void onClick(View v) {
//Set the text based on the selected button and send it to function vfShareQuote
    switch (v.getId()) {
    case id1:
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_1));
        break;
    case id2:
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_2));
        break;
    }
    }
};

因为
R.string.agafon_1
R.string.agafon_2
不是id。它们只是字符串资源的id。将id设置为按钮并使用它们。像

agafon_1.setId(id1);
agafon_2.setId(id2);
其中,id1和id2是两个int。并使用它们

OnClickListener oclShareQuote = new OnClickListener() {
    @Override
    public void onClick(View v) {
//Set the text based on the selected button and send it to function vfShareQuote
    switch (v.getId()) {
    case id1:
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_1));
        break;
    case id2:
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_2));
        break;
    }
    }
};

问题出在开关块上。。您没有为按钮设置Id,您正试图在单击时访问它们

试一试

然后在开关块中使用这些id

OnClickListener oclShareQuote = new OnClickListener() {
    @Override
    public void onClick(View v) {
//Set the text based on the selected button and send it to function vfShareQuote
    switch (v.getId()) {
    case 1://agafon_1 is clicked
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_1));
        break;
    case 2://agafon_2 is clicked
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_2));
        break;
    }
    }
};

问题出在开关块上。。您没有为按钮设置Id,您正试图在单击时访问它们

试一试

然后在开关块中使用这些id

OnClickListener oclShareQuote = new OnClickListener() {
    @Override
    public void onClick(View v) {
//Set the text based on the selected button and send it to function vfShareQuote
    switch (v.getId()) {
    case 1://agafon_1 is clicked
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_1));
        break;
    case 2://agafon_2 is clicked
        vfShareQuote(getResources().getText(R.string.name_agafon)+":\n"+getResources().getText(R.string.agafon_2));
        break;
    }
    }
};

以编程方式创建按钮时,应以以下方式为每个按钮提供id:

agafon_1.setId("btn id");
还有一个问题,因为一些android SDK的开关箱已经不起作用了,
您必须使用if语句进行设置。

以编程方式创建按钮时,您应该以以下方式为每个按钮提供id:

agafon_1.setId("btn id");
还有一个问题,因为一些android SDK的开关箱已经不起作用了,
您必须使用if语句。

我很好奇,当switch case不工作的问题被提出时,我/我们从来没有遇到过这种问题。我记不清当我收到这样一个警告,switch case不适用于此版本的android时,我正在使用的sdk的确切版本。可能eclipse是在开玩笑,但我必须将所有开关情况更改为if条件。我想我当时使用的是4.2.2版本,我很好奇,什么时候出现了switch case not working问题,我/我们从来没有遇到过这种问题……我记不清当我收到这样一个警告,switch case不适用于这个安卓版本时,我正在使用的sdk的确切版本。可能eclipse是在开玩笑,但我必须将所有开关情况更改为if条件。我想我是在用4.2.2版