标签:提交 length tools ppc xmlns err ted tool check
(一)
1,布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="helloworld.com.inspur.app5.MainActivity"> <TextView android:layout_width="wrap_content" android:id="@+id/tv" android:layout_height="wrap_content" android:text="你的爱好是:" /> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sing" android:text="唱歌"/> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/dance" android:text="跳舞"/> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/game" android:text="玩游戏"/> </LinearLayout>
2,逻辑代码
package helloworld.com.inspur.app5; import android.support.v4.widget.TextViewCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView tv; private CheckBox cb1; private CheckBox cb2; private CheckBox cb3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.tv); cb1=(CheckBox)findViewById(R.id.sing); cb2=(CheckBox)findViewById(R.id.dance); cb3=(CheckBox)findViewById(R.id.game); cb1.setOnCheckedChangeListener(new MyListener()); cb2.setOnCheckedChangeListener(new MyListener()); cb3.setOnCheckedChangeListener(new MyListener()); } class MyListener implements CompoundButton.OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch(buttonView.getId()) { case R.id.sing: Toast.makeText(MainActivity.this,"sing",Toast.LENGTH_SHORT).show(); break; case R.id.dance: Toast.makeText(MainActivity.this,"dance",Toast.LENGTH_SHORT).show(); break; case R.id.game: Toast.makeText(MainActivity.this,"game",Toast.LENGTH_SHORT).show(); break; } } } }
(3)if实现
if(isChecked) { Toast.makeText(MainActivity.this,buttonView.getText().toString()+":::"+((CheckBox)buttonView).isChecked(),Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(MainActivity.this,buttonView.getText().toString()+":::"+((CheckBox)buttonView).isChecked(),Toast.LENGTH_SHORT).show(); }
(4)
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="helloworld.com.inspur.app5.MainActivity"> <TextView android:layout_width="wrap_content" android:id="@+id/tv" android:layout_height="wrap_content" android:text="你的爱好是:" /> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sing" android:text="唱歌"/> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/dance" android:text="跳舞"/> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/game" android:text="玩游戏"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/but" android:text="提交"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_show"/> </LinearLayout>
逻辑实现
package helloworld.com.inspur.app5; import android.support.v4.widget.TextViewCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private TextView tv; private TextView tv_show; private CheckBox cb1; private CheckBox cb2; private CheckBox cb3; Button but; List<CheckBox> list; int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.tv); tv_show=(TextView)findViewById(R.id.tv_show); cb1=(CheckBox)findViewById(R.id.sing); cb2=(CheckBox)findViewById(R.id.dance); cb3=(CheckBox)findViewById(R.id.game); list=new ArrayList<>(); but= (Button)findViewById(R.id.but); list.add(cb1); list.add(cb2); list.add(cb3); but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuffer sb= new StringBuffer(); for(CheckBox cb : list) { if(cb.isChecked()) { sb.append(cb.getText().toString()); count++; } } if(sb!=null) { Toast.makeText(MainActivity.this,sb+",共"+count+"项",Toast.LENGTH_SHORT ).show(); count=0; } else{ Toast.makeText(MainActivity.this,sb+",共"+count+"项",Toast.LENGTH_SHORT ).show(); } } }); } }
标签:提交 length tools ppc xmlns err ted tool check
原文地址:https://www.cnblogs.com/excellencesy/p/9008443.html