如何根据我的选择更新我的Texview

时间:2015-06-12 16:13:50

标签: android performance

我有一个textview,2个Checkboxes,2个RadioButtons。我应该在textview上更新我选择或取消选择的项目 - 如何实现这一目标?我知道我们可以使用if if来做,但如果问题变得复杂怎么办?例如,如果我有10个复选框和10个单选按钮怎么办?我是否必须为所有视图写条件?

以下是我的代码的外观

public class MainActivity extends Activity {

TextView textview;
CheckBox checkbox1;
CheckBox checkbox2;
RadioGroup group1;
RadioButton radio1;
RadioButton radio2;
static String displayText="";


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

    textview = (TextView) findViewById(R.id.textView);

    checkbox1 = (CheckBox) findViewById(R.id.checkBox);
    checkbox2 = (CheckBox) findViewById(R.id.checkBox2);
    radio1 = (RadioButton) findViewById(R.id.radioButton);
    radio2 = (RadioButton) findViewById(R.id.radioButton2);

    group1 = (RadioGroup) findViewById(R.id.radio_group_1);
    checkbox1.setOnClickListener(new checkBoxClicked());
    checkbox2.setOnClickListener(new checkBoxClicked());


    radio1.setOnClickListener(new checkBoxClicked());
    radio2.setOnClickListener(new checkBoxClicked());
    textview.setText(displayText);
  class checkBoxClicked implements View.OnClickListener
{

 @Override
 public void onClick(View v) {

        if(radio1.isChecked())
        {
            displayText="radio1";
        }
        if(radio2.isChecked())
        {
            displayText="radio2";
        }

        if(checkbox1.isChecked())
        {
            displayText= "checkbox1";
        }
        if(checkbox2.isChecked())
        {
            displayText="checkbox2";

        }
        if(checkbox1.isChecked()&&checkbox2.isChecked())
        {
            displayText="checkbox1" + "checkbox2";

        }
        if(checkbox1.isChecked()&&radio1.isChecked())
        {
            displayText="checkbox1"+"radio1";
        }
        if(checkbox1.isChecked()&&radio2.isChecked())
        {
            displayText="checkbox1"+"radio2";
        }
        if(checkbox2.isChecked()&&radio1.isChecked())
        {
            displayText="checkbox2"+"radio1";
        }
        if(checkbox2.isChecked()&&radio2.isChecked())
        {
            displayText="checkbox2"+"radio2";
        }
        if(radio1.isChecked()&&checkbox1.isChecked()&&checkbox2.isChecked())
        {
            displayText="radio1"+"checkbox1"+"checkbox2";
        }
        if(radio2.isChecked()&&checkbox1.isChecked()&&checkbox2.isChecked())
        {
            displayText="radio2"+"checkbox1"+"checkbox2";
        }

        if(!checkbox1.isChecked()&&!checkbox2.isChecked()&&!radio1.isChecked()&&!radio2.isChecked())
        {
            displayText="";
        }
       textview.setText(displayText);
 }}}

4 个答案:

答案 0 :(得分:1)

给它一个镜头..它应该给你与你的代码相同的功能(除了无线电值总是在displayText字符串的前面),代码少得多。

 // GET: CreateBundlesAndCartons
    public ActionResult CreateBandC(Int32 id)
    {
        string ReturnMessage;
        ReturnMessage = "";
        using (SqlConnection connection = new SqlConnection())
        {
            //string connectionStringName = this.DataWorkspace.CooperData.Details.Name;
            connection.ConnectionString =
                ConfigurationManager.ConnectionStrings["PSAContext"].ConnectionString;
            string procedure = "PSA.dbo.CreateBundlesAndCartons";
            using (SqlCommand command = new SqlCommand(procedure, connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandTimeout = 300;

                command.Parameters.Add(
                    new SqlParameter("@JobID", id));
                SqlParameter ErrorString = new SqlParameter("@ErrorString", ReturnMessage);
                ErrorString.Direction = ParameterDirection.Output;
                ErrorString.Size = 4000;
                command.Parameters.Add(ErrorString);

                connection.Open();
                command.ExecuteNonQuery();

                // Save Outout Param
                ReturnMessage = ErrorString.Value.ToString();

            }
        }
        return Content("You requested the to create bundles and cartons for job ID " + id.ToString() + "<br />Result: " + ReturnMessage + "<br /> ");
    }

答案 1 :(得分:0)

我会使用你现在拥有的if-else案例,但是为每个不同的元组使用StringBuffer而不是case

        StringBuffer sb = new StringBuffer();
        if(radio1.isChecked())
        {
            sb.append("radio1");
        }
        if(radio2.isChecked())
        {
            sb.append("radio2");
        }

        if(checkbox1.isChecked())
        {
            sb.append("checkbox1");
        }
        if(checkbox2.isChecked())
        {
            sb.append("checkbox2");
        }
        textview.setText(sb.toString());

这样你每个复选框/ radiobutton只需要1个案例

答案 2 :(得分:0)

在您的主要活动中:

LinearLayout layout;
TextView textview;
CheckBox checkbox1;
CheckBox checkbox2;
RadioGroup group1;
RadioButton radio1;
RadioButton radio2;
static String displayText="";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

layout = (LinearLayout) findViewByID(R.id.linearlayout);
StringBuffer sb = new StringBuffer();
checkbox1 = (CheckBox) findViewById(R.id.checkBox);
checkbox2 = (CheckBox) findViewById(R.id.checkBox2);
radio1 = (RadioButton) findViewById(R.id.radioButton);
radio2 = (RadioButton) findViewById(R.id.radioButton2);
textview = (TextView) findViewById(R.id.textView);
}

onClick(假设linearlayout是所有单选按钮和复选框的父级):

public void onClick() { 
StringBuffer sb = new StringBuffer();
for (int i=0; i < layout.getChildCount(); i++){
        View v = layout.getChildAt(i);
        if (v instanceOf CompoundButton) { 
           if (v.isChecked) {
              sb.append(v.getText());
           }
        }

}
textView.setText(sb.toString());
}

答案 3 :(得分:0)

定义这三种方法:

private String getText(RadioButton button) {
    if button.isChecked() return button.getText();
    else return "";
}

private String getText(CheckBox box) {
    if box.isChecked() return "checkBox"
    else return "";
}

private void refreshText() {
    TextView t;
    String text = getText(button1) + getText(button2) + getText(checkbox1) + getText(checkbox2);
    t.setText(text);
}

然后,您的每个观点都应该只需点击refreshText()即可。