源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android CheckBox 的使用案例分析

  • 时间:2020-08-03 00:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android CheckBox 的使用案例分析
[u]复制代码[/u] 代码如下:
public class MainActivity extends Activity {  TextView tv;  CheckBox cb1;  CheckBox cb2;  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   cb1 = (CheckBox) findViewById(R.id.checkbox1);   cb2 = (CheckBox) findViewById(R.id.checkbox2);   tv = (TextView) findViewById(R.id.textview1);   cb1.[b]setOnCheckedChangeListener[/b](cb);   cb2.[b]setOnCheckedChangeListener[/b](cb);  }  private CheckBox.OnCheckedChangeListener cb = new CheckBox.OnCheckedChangeListener() {   @Override   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    String stv = getString(R.string.hoby);    String scb1 = getString(R.string.basketball);    String scb2 = getString(R.string.football);    if (cb1.isChecked() && cb2.isChecked()) {     tv.setText(stv + ":" + scb1 + "&&" + scb2);    } else if (cb1.isChecked() && !cb2.isChecked()) {     tv.setText(stv + ":" + scb1);    } else if (!cb1.isChecked() && cb2.isChecked()) {     tv.setText(stv + ":" + scb2);    } else {     tv.setText(stv);    }   }  };
[b]如下是布局文件: [/b]
[u]复制代码[/u] 代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >     <TextView         android:id="@+id/textview1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/hoby" />     <CheckBox         android:id="@+id/checkbox1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/basketball" />     <CheckBox         android:id="@+id/checkbox2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/football" /> </LinearLayout>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部