arrayadapter+simpleadapter
1 public class MainActivity extends Activity { 2 3 private ListView list;//视图,即为电视屏幕 4 private ArrayAdapter<String> arradapter;//数据适配器,适用于数组+集合,即为无线 5 private SimpleAdapter simadapter; 6 private List<Map<String, Object>> datalist; 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 list =(ListView)findViewById(R.id.listView1);//数组形式的数据源,即为电视台 13 //1.新建一个数据适配器 14 //.SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) 15 //2.适配器加载适配源 16 String [] arrdata={"mook1","i","like","u"}; 17 arradapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrdata); 18 19 datalist=new ArrayList<Map<String,Object>>(); 20 simadapter=new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text}); 21 //3.视图加载适配器 22 //list.setAdapter(arradapter); 23 list.setAdapter(simadapter); 24 25 } 26 private List<Map<String, Object>> getdata(){ 27 for (int i=0;i<20;i++){ 28 Map<String , Object> map=new HashMap<String, Object>(); 29 map.put("pic", R.drawable.ic_launcher); 30 map.put("text", "mook"+i); 31 datalist.add(map); 32 } 33 return datalist; 34 } 35 36 }
spinner
1 public class MainActivity extends Activity { 2 private TextView text1,text2,text3; 3 private EditText edit; 4 private Spinner spin; 5 private ArrayAdapter<String> arr_adapter; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 text1=(TextView)findViewById(R.id.textView1); 12 text2=(TextView)findViewById(R.id.textView2); 13 text3=(TextView)findViewById(R.id.textView3); 14 15 16 spin=(Spinner)findViewById(R.id.spinner); 17 edit=(EditText)findViewById(R.id.editText1); 18 19 //1、新建适配器 2、适配器加载数据源 3、视图加载适配器 20 String[] data=new String[]{"小学","初中","高中","本科","研究生"}; 21 arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data); 22 arr_adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 23 spin.setAdapter(arr_adapter); 24 25 spin.setOnItemSelectedListener(listener); 26 27 28 } 29 private Spinner.OnItemSelectedListener listener= 30 new Spinner.OnItemSelectedListener() { 31 32 @Override 33 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 34 // TODO Auto-generated method stub 35 String s1=edit.getText().toString(); 36 String s2=spin.getSelectedItem().toString(); 37 38 text3.setText(s1+"的学历:"+s2); 39 } 40 41 @Override 42 public void onNothingSelected(AdapterView<?> parent) { 43 // TODO Auto-generated method stub 44 45 } 46 }; 47 48 49 }
Android攻城狮的第一门课(入门篇)
第5章 认识按钮Button以及ImageButton
text属性内容放置在value-string.xml中
加注释快捷键:
ctrl+/ //注释
ctrl+shift+/ /**/注释
监听三种:
1 public class MainActivity extends Activity implements OnClickListener{ 2 3 4 private Button btn; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10 btn=(Button)findViewById(R.id.button1); 11 /*监听的三种方式 12 * 1、匿名内部类 13 * 2、外部类 14 * 3.接口 15 */ 16 17 /* 1 18 * btn.setOnClickListener(new OnClickListener() { 19 20 @Override 21 public void onClick(View v) { 22 // TODO 自动生成的方法存根 23 Log.i("tag", "匿名内部类"); 24 //Toast.makeText(MainActivity.this, "匿名", Toast.LENGTH_LONG); 25 } 26 });*/ 27 28 //btn.setOnClickListener(listener); 29 btn.setOnClickListener(this); 30 } 31 /*2 32 * OnClickListener listener =new OnClickListener() { 33 34 @Override 35 public void onClick(View v) { 36 // TODO 自动生成的方法存根 37 Log.i("tag", "匿名内部类"); 38 Toast.makeText(MainActivity.this, "匿名", Toast.LENGTH_LONG); 39 } 40 };*/ 41 @Override 42 public void onClick(View v) { 43 // TODO 自动生成的方法存根 44 Log.i("tag", "匿名内部类"); 45 46 } 47 48 49 50 51 52 53 54 55 }
textview实现跑马灯效果
1 <com.example.meng.marqueettext 2 android:id="@+id/textView2" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:singleLine="true"//单行 6 android:ellipsize="marquee"//去掉省略号 7 android:focusable="true"//使其循环 8 android:marqueeRepeatLimit="marquee_forever" 9 //无限制循环 10 android:focusableInTouchMode="true" 11 android:text="@string/text_name"/> 12 13 14 <com.example.meng.marqueettext 15 android:id="@+id/textView1" 16 android:layout_marginTop="10dp" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:singleLine="true" 20 android:ellipsize="marquee" 21 android:focusable="true" 22 android:marqueeRepeatLimit="marquee_forever" 23 android:focusableInTouchMode="true" 24 android:text="@string/text_name"/>
step1:textview增加若干属性,即可实现一个textview的跑马灯效果
step2 : 复制一个textview,两个的情况下必须建立新的类marquee,继承textview,三个构造方法+isfucused方法return TRUE
step3:包名+类名代替textview
动态匹配功能:显示的下拉列表,使用arrayadapter
1 private AutoCompleteTextView act; 2 private ArrayAdapter<String> arr_adapter; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 act=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 8 String[] resource={"beijing1","beijing2","beijing3","shanghai1","shanghai2"}; 9 arr_adapter =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 10 resource); 11 act.setAdapter(arr_adapter); 12 }
注意两条属性:
提示
输入几个字母开始提示
1 android:hint="请输入你要搜索的关键词" 2 android:completionThreshold="3"
与autocompletetextview不同的是,可以用逗号作为分隔符,进行多选。
1 private ArrayAdapter<String> arr_adapter; 2 private MultiAutoCompleteTextView mct; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 mct=(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); 9 10 11 String[] resource={"beijing1","beijing2","beijing3","shanghai1","shanghai2"}; 12 arr_adapter =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 13 resource); 14 15 mct.setAdapter(arr_adapter); 16 mct.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 17 18 }
1 private ToggleButton tb; 2 private ImageView iv; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 tb=(ToggleButton) findViewById(R.id.toggleButton1); 9 iv=(ImageView) findViewById(R.id.imageView3); 10 11 tb.setOnCheckedChangeListener(this); 12 } 13 @Override 14 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 15 //当tb被点击时,执行当前的方法 16 //第一个参数检测是哪个tb被点击了,第二个参数判断当前tb的状态 17 iv.setBackgroundResource(isChecked?R.drawable.open:R.drawable.close); 18 19 }
1 private CheckBox cb; 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 7 cb=(CheckBox) findViewById(R.id.checkBox1); 8 cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 9 10 @Override 11 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 12 // 匿名内部类实现监听,通过该方法来监听CheckBox是否被选中 13 //ischecked参数可以判断当前的cb是否被选中 14 if (isChecked) { 15 String text= cb.getText().toString(); 16 Log.i("tag", text);//在控制台的日志中显示text 17 } 18 19 } 20 });
1 public class MainActivity extends Activity implements android.widget.RadioGroup.OnCheckedChangeListener { 2 3 private RadioGroup rg; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 rg=(RadioGroup) findViewById(R.id.radioGroup1); 9 rg.setOnCheckedChangeListener(this); 10 11 12 } 13 14 @Override 15 public void onCheckedChanged(RadioGroup group, int checkedId) { 16 switch (checkedId) { 17 case R.id.radio1: 18 Log.i("tag", "men"); 19 break; 20 case R.id.radio2: 21 Log.i("tag", "women"); 22 break; 23 default: 24 break; 25 } 26 27 } 28 29 30 31 }
使用接口时,继承的是android.widget.RadioGroup.OnCheckedChangeListener
若直接写oncheckedchangelistener,必须要导入对应的包。
radiogroup是单选,可以用switch语句
gravity决定其子类控件的属性
写在线性布局中。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="bottom|center_horizontal" 6 > 7 8 <Button 9 android:id="@+id/button1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="Button" /> 13 14 </LinearLayout>
layout_gravity决定的是当前父类控件的位置。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 6 > 7 8 <Button 9 android:id="@+id/button1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="Button" 13 android:layout_gravity="center_horizontal|center"/> 14 15 </LinearLayout>
界面的布局,往往需要两种属性共同控制,父类和子类。
如果父类和子类中的某些属性相悖,当然是听爸爸的啦~
layout_weight 子类控件在父类控件中所占比例。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 > 7 8 <Button 9 android:id="@+id/button2" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="Button" 13 android:layout_weight="1" /> 14 15 <Button 16 android:id="@+id/button1" 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:text="Button" 20 android:layout_weight="1" 21 /> 22 23 </LinearLayout>