【问题标题】:android studio value check box, only checked send from messageandroid studio value 复选框,仅选中从消息发送
【发布时间】:2017-05-20 19:11:22
【问题描述】:

我是 android studio 的新手,正在尝试发送一个仅通过 SMS 消息检查的值复选框。

我将值更改为String,因此尽管未选中所有值复选框都已发送

请帮我修复程序,以便仅选中的值复选框通过SMS 消息发送。

完整代码:

private EditText qty1, qty2,qty3,qty4,qty5,qty6;
private CheckBox cos5,cos10,cos20,cos25,cos50,cos100;
private Spinner spinnerrs;
private Button buttonsendtrx;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trx);

    qty1 = (EditText)findViewById(R.id.editTextQty1);
    qty2 = (EditText)findViewById(R.id.editTextQty2);
    qty3 = (EditText)findViewById(R.id.editTextQty3);
    qty4 = (EditText)findViewById(R.id.editTextQty4);
    qty5 = (EditText)findViewById(R.id.editTextQty5);
    qty6 = (EditText)findViewById(R.id.editTextQty6);
    cos5 =(CheckBox)findViewById(R.id.S05);
    cos10=(CheckBox)findViewById(R.id.S10);
    cos20=(CheckBox)findViewById(R.id.S20);
    cos25=(CheckBox)findViewById(R.id.S25);
    cos50=(CheckBox)findViewById(R.id.S50);
    cos100=(CheckBox)findViewById(R.id.S100);

    buttonsendtrx = (Button) findViewById(R.id.buttonsendtrx);
    addItemOnSpinner();

     buttonsendtrx.setOnClickListener(new View.OnClickListener(){
         public void onClick(View view) {
            CheckBox cb = (CheckBox) view;

            if (view==buttonsendtrx);
             {
                 sendsms();
             }
         }
     });
    }


public void sendsms () {

    Log.i("Send SMS","");
    String nomor = spinnerrs.getSelectedItem().toString();
    String jumlah1 = qty1.getText().toString();
    String jumlah2 = qty2.getText().toString();
    String jumlah3 = qty3.getText().toString();
    String jumlah4 = qty4.getText().toString();
    String jumlah5 = qty5.getText().toString();
    String jumlah6 = qty6.getText().toString();
    String s5 = cos5.getText().toString();
    String s10 = cos10.getText().toString();
    String s20 = cos20.getText().toString();
    String s25 = cos25.getText().toString();
    String s50 = cos50.getText().toString();
    String s100 = cos100.getText().toString();


    {
        try {
            SmsManager smsManager = SmsManager.getDefault();

            smsManager.sendTextMessage(nomor, null, s5+jumlah1+s10+jumlah2+s20+jumlah3+s25+jumlah4+s50+jumlah5+s100++jumlah6, null, null);
            Toast.makeText(getApplicationContext(), "SMS SEND", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS FAILED,PLEASE TRY AGAIN", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }
}

public void addItemOnSpinner() {
    spinnerrs = (Spinner) findViewById(R.id.spinnerrs);
    List<String> kategori = new ArrayList<String>();
    kategori.add("08127648200");
    kategori.add("081276482000");
    kategori.add("081275250000");
    ArrayAdapter<String> dataadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,kategori);
    dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerrs.setAdapter(dataadapter);
}

public void selectItem(查看视图) {

    boolean checked = ((CheckBox) view).isChecked();

    switch (view.getId())
    {
        case R.id.S05:

        if (checked){
            qty1.setBackgroundColor(Color.parseColor("#FFFFFF"));
            qty1.setFocusable(true);
            qty1.setFocusableInTouchMode(true);
            qty1.setCursorVisible(true);
            qty1.setEnabled(true);
            qty1.setVisibility(view.VISIBLE);
            break;
        }
        else {
            //selection.remove(String.valueOf(cos5));
            qty1.setBackgroundColor(Color.parseColor("#CCCCCC"));
            //qty1.setTextIsSelectable(false);
            qty1.setFocusable(false);
            qty1.setFocusableInTouchMode(false);
            qty1.setCursorVisible(false);
            qty1.setEnabled(false);
            qty1.setVisibility(view.INVISIBLE);
            break;
        }

        case R.id.S10:
            if (checked){
                qty2.setBackgroundColor(Color.parseColor("#FFFFFF"));

                qty2.setFocusable(true);
                qty2.setFocusableInTouchMode(true);
                qty2.setCursorVisible(true);
                qty2.setEnabled(true);
                qty2.setVisibility(view.VISIBLE);

                break;
            }
            else {
                qty2.setBackgroundColor(Color.parseColor("#CCCCCC"));

                qty2.setFocusable(false);
                qty2.setFocusableInTouchMode(false);
                qty2.setCursorVisible(false);
                qty2.setEnabled(false);
                qty2.setVisibility(view.INVISIBLE);
                break;
            }

        case R.id.S20:
            if (checked){
                qty3.setBackgroundColor(Color.parseColor("#FFFFFF"));

                qty3.setFocusable(true);
                qty3.setFocusableInTouchMode(true);
                qty3.setCursorVisible(true);
                qty3.setEnabled(true);
                qty3.setVisibility(view.VISIBLE);

                break;
            }
            else {
                qty3.setBackgroundColor(Color.parseColor("#CCCCCC"));

                qty3.setFocusable(false);
                qty3.setFocusableInTouchMode(false);
                qty3.setCursorVisible(false);
                qty3.setEnabled(false);
                qty3.setVisibility(view.INVISIBLE);
                break;
            }

        case R.id.S25:
            if (checked){
                qty4.setBackgroundColor(Color.parseColor("#FFFFFF"));

                qty4.setFocusable(true);
                qty4.setFocusableInTouchMode(true);
                qty4.setCursorVisible(true);
                qty4.setEnabled(true);
                qty4.setVisibility(view.VISIBLE);

                break;
            }
            else {
                qty4.setBackgroundColor(Color.parseColor("#CCCCCC"));
                //qty4.setTextIsSelectable(false);
                qty4.setFocusable(false);
                qty4.setFocusableInTouchMode(false);
                qty4.setCursorVisible(false);
                qty4.setEnabled(false);
                qty4.setVisibility(view.INVISIBLE);
                break;
            }
            case R.id.S50:
            if (checked){
                qty5.setBackgroundColor(Color.parseColor("#FFFFFF"));
                //qty5.setTextIsSelectable(true);
                qty5.setFocusable(true);
                qty5.setFocusableInTouchMode(true);
                qty5.setCursorVisible(true);
                qty5.setEnabled(true);
                qty5.setVisibility(view.VISIBLE);
                break;
            }
            else {
                qty5.setBackgroundColor(Color.parseColor("#CCCCCC"));
                qty5.setFocusable(false);
                qty5.setFocusableInTouchMode(false);
                qty5.setCursorVisible(false);
                qty5.setEnabled(false);
                qty5.setVisibility(view.INVISIBLE);
                break;
            }
        case R.id.S100:
            if (checked){
                qty6.setBackgroundColor(Color.parseColor("#FFFFFF"));
                qty6.setFocusable(true);
                qty6.setFocusableInTouchMode(true);
                qty6.setCursorVisible(true);
                qty6.setEnabled(true);
                qty6.setVisibility(view.VISIBLE);
                break;
            }
            else {
                qty6.setBackgroundColor(Color.parseColor("#CCCCCC"));
                qty6.setFocusable(false);
                qty6.setFocusableInTouchMode(false);
                qty6.setCursorVisible(false);
                qty6.setEnabled(false);
                qty6.setVisibility(view.INVISIBLE);
                break;
            }

    }

  }

}

【问题讨论】:

  • 您的具体问题是什么?
  • 请...如何修复程序,以便仅选中的值复选框通过短信发送。谢谢

标签: android checkbox


【解决方案1】:
public void sendsms () {

Log.i("Send SMS","");
String nomor = spinnerrs.getSelectedItem().toString();
String jumlah1 = qty1.getText().toString();
String jumlah2 = qty2.getText().toString();
String jumlah3 = qty3.getText().toString();
String jumlah4 = qty4.getText().toString();
String jumlah5 = qty5.getText().toString();
String jumlah6 = qty6.getText().toString();

if(cos5.isChecked){
String s5 = cos5.getText().toString();
return;
}
if(cos10.isChecked){
String s10 = cos10.getText().toString();
return;
}
if(cos20.isChecked){
String s20 = cos20.getText().toString();
return;
}
if(cos25.isChecked){
String s25 = cos25.getText().toString();
return;
}
if(cos50.isChecked){
String s50 = cos50.getText().toString();
return;
}
if(cos100.isChecked){
String s100 = cos100.getText().toString();
return;
}


try {
        SmsManager smsManager = SmsManager.getDefault();

        smsManager.sendTextMessage(nomor, null, s5+jumlah1+s10+jumlah2+s20+jumlah3+s25+jumlah4+s50+jumlah5+s100++jumlah6, null, null);
        Toast.makeText(getApplicationContext(), "SMS SEND", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS FAILED,PLEASE TRY AGAIN", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }


}

【讨论】:

  • W4R10CK。我试过这个。但据说无法解析符号“isChecked”。我该怎么办?谢谢
  • 有解析符号'isChecked'。但短信管理器中的 s5,s10,s20,s25,s50,s100 仍然出错。它是红色的。错误无法解析 s5,
  • 在这里:smsManager.sendTextMessage(nomor, null, s5+jumlah1+s10+jumlah2+s20+jumlah3+s25+jumlah4+s50+jumlah5+s100++jumlah6, null, null); //还是没有解决
  • 是的,尝试在 if 条件之外定义 s5,s10... 字符串。喜欢String s5; String s10 和里面如果使用s5 = cos5.getText().toString(); 等等。
  • 请帮帮我.. 有 3 周无法解决这个问题。谢谢
【解决方案2】:

首先查找复选框是否被选中,并将其添加到字符串中,如下所示:-

 String msg_to_send="";
    if(cos5.isChecked()){
    msg_to_send = msg_to_send+cos5.getText().toString();
    }
    if(cos10.isChecked()){
    msg_to_send = msg_to_send+cos10.getText().toString();
    }
    if(cos20.isChecked()){
    msg_to_send = msg_to_send+ cos20.getText().toString();
    }
    if(cos25.isChecked()){
    msg_to_send = msg_to_send+cos25.getText().toString();
    }
    if(cos50.isChecked()){
   msg_to_send = msg_to_send+ cos50.getText().toString();
    }
    if(cos100.isChecked()){
   msg_to_send = msg_to_send+cos100.getText().toString();

    }

将此字符串发送到您的短信:-

 smsManager.sendTextMessage(nomor, null, msg_to_send, null, null);//you can add your editText also

【讨论】:

  • 它也不起作用。运行时出错。不幸的是android停止了
  • 你遇到了什么异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多