R.string为我提供了数字

时间:2016-04-30 07:38:29

标签: android android-resources

我有这个代码无效:

公共类MainActivity扩展了AppCompatActivity {

int quantity=2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i( "MainActivity", "modriodfw" + (R.string.Thankyou));
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    EditText nombre = (EditText) findViewById(R.id.nombre);
    String nombre1 = nombre.getText().toString();
    boolean cream = getstate();
    boolean chocolate = getcState();
    int price = calculatePrice(cream, chocolate);


    String summary = createOrderSummary(price, cream,chocolate,nombre1);

    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_SUBJECT, (R.string.OrderMail) + nombre1);
    intent.putExtra(Intent.EXTRA_TEXT, summary);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }

    displayMessage(summary);

}
public void clearText(View view)
{
    EditText nombre = (EditText) findViewById(R.id.nombre);
    nombre.setText("");
}
private  boolean getcState()
{
    CheckBox state = (CheckBox) findViewById(R.id.chocolate);
    boolean chocolateState = state.isChecked();
    return chocolateState;
}
private  boolean getstate()
{
    CheckBox state = (CheckBox) findViewById(R.id.cream);
    boolean creamState = state.isChecked();
    return (creamState);
}
private String createOrderSummary(int price, boolean cream, boolean chocolate,String nombre1)
{
        String summary = (R.string.name) + nombre1;
    if(cream && chocolate == false){
        summary += "\n" + quantity + (R.string.name);
    }
    if(chocolate && cream == false){
        summary += "\n" + quantity + (R.string.SummaryCream);
    }

    if(chocolate && cream){
        summary += "\n" + quantity + (R.string.SummaryBoth);
    }
    summary += "\nTotal: $" +price;
    summary += "\n" + (R.string.Thankyou);
        return summary;
}

private int calculatePrice(boolean cream, boolean chocolate) {
   int price = 5;
    if (cream) {
        price = price + 1;
    }
    if (chocolate) {
        price = price + 2;
    }
    price = price * quantity;
    return price;
}

public void increase(View view) {
    if (quantity == 99){
        Toast.makeText(this, (R.string.high), Toast.LENGTH_SHORT).show();
        return;

    }
    quantity= quantity + 1;
    display(quantity);

}

public void decrease(View view){
    if (quantity == 1){
        Toast.makeText(this, (R.string.less), Toast.LENGTH_SHORT).show();
        return;
    }
        quantity= quantity - 1;
    display(quantity);

}
/**
 * This method displays the given quantity value on the screen.
 */
private void display(int numb) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + numb);
}

/**
 * This method displays the given text on the screen.
 */
private void displayMessage(String Summary) {
    TextView summaryTextView = (TextView) findViewById(R.id.Summary_text_view);
    summaryTextView.setText(Summary);
}

我得到的就是这个:

2131099680 总计:10美元 2131099676

它应该是

名称: 摘要 总计$ 谢谢!

我在android studio中使用java。

2 个答案:

答案 0 :(得分:1)

您需要执行context.getString(R.string.your_string),因为R.string.your_string本身只是一个参考。

答案 1 :(得分:1)

您忘记在四个地方调用getString方法。我更新了你的代码:

  private String createOrderSummary(int price, boolean cream, boolean chocolate,String nombre1)
    {
            String summary = getString(R.string.name) + nombre1;
        if(cream && chocolate == false){
            summary += "\n" + quantity + getString(R.string.SummaryChocolate);
        }
        if(chocolate && cream == false){
            summary += "\n" + quantity + getString(R.string.SummaryCream);
        }

        if(chocolate && cream){
            summary += "\n" + quantity + getString(R.string.SummaryBoth);
        }
        summary += "\nTotal: $" +price;
        summary += "\n" + getString(R.string.Thankyou);
            return summary;
    }`enter code here`