01 | package com.ecs.wenchen.dynamiccheckbox; |
03 | import java.util.Iterator; |
04 | import java.util.LinkedHashMap; |
08 | import android.app.Activity; |
09 | import android.os.Bundle; |
10 | import android.view.View; |
11 | import android.widget.Button; |
12 | import android.widget.CheckBox; |
13 | import android.widget.LinearLayout; |
14 | import android.widget.TextView; |
15 | import android.widget.EditText; |
16 | import android.graphics.Color; |
18 | public class MainActivity extends Activity { |
19 | LinearLayout linearBox; |
20 | CheckBox checkBox, checkBox1; |
23 | protected void onCreate( Bundle savedInstanceState ) { |
24 | super .onCreate( savedInstanceState ); |
25 | setContentView( R.layout.activity_main ); |
27 | linearBox = (LinearLayout) findViewById( R.id.linearBox ); |
28 | final EditText number = (EditText) findViewById( R.id.number ); |
29 | final EditText title = (EditText) findViewById( R.id.title ); |
30 | final Button button = (Button) findViewById( R.id.add ); |
32 | button.setOnClickListener( new View.OnClickListener( ) { |
33 | public void onClick( View v ) { |
34 | checkBox1 = new CheckBox( getApplicationContext( ) ); |
35 | checkBox1.setId( Integer.parseInt( number.getText( ).toString( ) ) ); |
36 | checkBox1.setText( title.getText( ).toString( ) ); |
37 | checkBox1.setTextColor( Color.BLACK ); |
38 | checkBox1.setOnClickListener( getOnClickDoSomething( checkBox1 ) ); |
39 | linearBox.addView( checkBox1 ); |
43 | final LinkedHashMap<String, String> |
44 | alphabet = new LinkedHashMap<String, String>( ); |
45 | alphabet.put( "260" , ".NET" ); |
46 | alphabet.put( "370" , "Computer Architecture" ); |
48 | Set<?> set = alphabet.entrySet( ); |
50 | Iterator<?> i = set.iterator( ); |
52 | while ( i.hasNext( ) ) { |
53 | @SuppressWarnings ( "rawtypes" ) |
54 | Map.Entry me = ( Map.Entry ) i.next( ); |
55 | checkBox = new CheckBox( this ); |
56 | checkBox.setId( Integer.parseInt( me.getKey( ).toString( ) ) ); |
57 | checkBox.setText( me.getValue( ).toString( ) ); |
58 | checkBox.setOnClickListener( getOnClickDoSomething( checkBox ) ); |
59 | linearBox.addView( checkBox ); |
63 | View.OnClickListener getOnClickDoSomething( final Button button ) { |
64 | return new View.OnClickListener( ) { |
65 | public void onClick( View v ) { |
66 | final TextView tvView = (TextView) findViewById( R.id.textView3 ); |
67 | tvView.setText( button.getId( ) + ": " + button.getText( ).toString( ) ); |
|