Android(4.X)学习笔记
Activity启动流程
Android操作系统 ---> AndroidManifest.xml --->MainAcitivity.onCreate() --->activity_main.xml ...
sp 字体大小会随系统设置的改变而变
dp 字体大小不会随系统设置的改变而变
Ctrl+shift+O 自动导入
Fragment的知识特别重要
<LinearLayoutxmlns: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:background="#440000"android:orientation="horizontal"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:layout_marginRight="20dp"android:background="#ff0000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="first"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="second"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:background="#ff0000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="first"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="second"/></LinearLayout></LinearLayout>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ff0000"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#00ff00"android:text="first"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#0000ff"android:text="second"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ff0000"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="#00ff00"android:text="first"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:background="#0000ff"android:layout_weight="2"android:text="second"/></LinearLayout></LinearLayout>
<RelativeLayoutxmlns: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:padding="15dp"><TextViewandroid:id="@+id/lableView"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="登陆界面"/><EditTextandroid:id="@+id/usernameText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:hint="username"android:layout_below="@id/lableView"/><EditTextandroid:id="@+id/passwordText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/usernameText"android:layout_alignRight="@id/usernameText"android:hint="password"android:inputType="textPassword"android:layout_below="@id/usernameText"/><Buttonandroid:id="@+id/cancelButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwordText"android:layout_alignParentRight="true"android:text="取消"/><Buttonandroid:id="@+id/okButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@id/cancelButton"android:layout_toLeftOf="@id/cancelButton"android:text="确定"/></RelativeLayout>
<AnalogClockandroid:id="@+id/clock"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
<DatePickerandroid:id="@+id/firstDatePicker"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/firstDatePicker"android:text="获取DatePicker的值"/>
publicclassMainActivityextendsActivity{privateDatePickerdatePicker;privateButtonbutton;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);datePicker=(DatePicker)findViewById(R.id.firstDatePicker);datePicker.updateDate(2013,4,10);button=(Button)findViewById(R.id.button);ButtonListenerbuttonListener=newButtonListener();button.setOnClickListener(buttonListener);}classButtonListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){inty=datePicker.getYear();intm=datePicker.getMonth();intd=datePicker.getDayOfMonth();System.out.println("y:"+y+",m"+m+",d:"+d);Toast.makeText(MainActivity.this,"y:"+y+",m"+m+",d:"+d,Toast.LENGTH_SHORT).show();}}}
<TimePickerandroid:id="@+id/firstTimePicker"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/firstTimePicker"android:text="获取TimePicker的值"/>
publicclassMainActivityextendsActivity{privateTimePickerfirstTimePicker;privateButtonbutton;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);firstTimePicker=(TimePicker)findViewById(R.id.firstTimePicker);button=(Button)findViewById(R.id.button);//该函数用于设置是否使用24小时制显示时间firstTimePicker.setIs24HourView(true);TimeListenertimeListenter=newTimeListener();firstTimePicker.setOnTimeChangedListener(timeListenter);ButtonListenerbuttonListener=newButtonListener();button.setOnClickListener(buttonListener);}classButtonListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){inthour=firstTimePicker.getCurrentHour();intminute=firstTimePicker.getCurrentMinute();Toast.makeText(MainActivity.this,"h:"+hour+",minute:"+minute,Toast.LENGTH_SHORT).show();}}classTimeListenerimplementsOnTimeChangedListener{/***view:该对象代表着TimePicker*hourOfDay:用户所选择的小时*minute:用户所选择的分钟*/@OverridepublicvoidonTimeChanged(TimePickerview,inthourOfDay,intminute){System.out.println("Hour:"+hourOfDay+",minute:"+minute);}}}
<ProgressBarandroid:id="@+id/firstProgressBar"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/firstButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/firstProgressBar"android:text="增加第一进度"/><Buttonandroid:id="@+id/secondButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/firstButton"android:text="增加第二进度"/>
publicclassMainActivityextendsActivity{privateProgressBarprogressBar;privateButtonfirstButton;privateButtonsecondButton;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);progressBar=(ProgressBar)findViewById(R.id.firstProgressBar);firstButton=(Button)findViewById(R.id.firstButton);secondButton=(Button)findViewById(R.id.secondButton);progressBar.setMax(100);firstButton.setOnClickListener(newFirstListener());secondButton.setOnClickListener(newSecondListener());}classFirstListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){progressBar.incrementProgressBy(10);}}classSecondListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){progressBar.incrementSecondaryProgressBy(20);}}}
<SeekBarandroid:id="@+id/firstSeekBar"android:layout_width="match_parent"android:layout_height="wrap_content"android:max="100"/>
publicclassMainActivityextendsActivity{privateSeekBarseekBar;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);seekBar=(SeekBar)findViewById(R.id.firstSeekBar);seekBar.setProgress(30);seekBar.setSecondaryProgress(50);SeekBarListenerlistener=newSeekBarListener();seekBar.setOnSeekBarChangeListener(listener);}classSeekBarListenerimplementsOnSeekBarChangeListener{/***seekBar该对象指的是触发了监听器的SeekBar对象*progress指的是当前SeekBar的进度*fromUser*/@OverridepublicvoidonProgressChanged(SeekBarSeekBar,intprogress,booleanfromUser){System.out.println("progress:"+progress+",fromUser:"+fromUser);Toast.makeText(MainActivity.this,"progress:"+progress+",fromUser:"+fromUser,Toast.LENGTH_SHORT).show();}@OverridepublicvoidonStartTrackingTouch(SeekBarseekBar){System.out.println("onStart");}@OverridepublicvoidonStopTrackingTouch(SeekBarseekBar){System.out.println("onStop");}}}
<RatingBarandroid:id="@+id/firstRatingBar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:numStars="4"android:stepSize="1"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/firstRatingBar"android:text="button"/>
publicclassMainActivityextendsActivity{privateRatingBarratingBar;privateButtonbutton;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ratingBar=(RatingBar)findViewById(R.id.firstRatingBar);button=(Button)findViewById(R.id.button);RatingBarListenerlistener=newRatingBarListener();ratingBar.setOnRatingBarChangeListener(listener);ButtonListenerbuttonListener=newButtonListener();button.setOnClickListener(buttonListener);}classButtonListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){ratingBar.setRating(ratingBar.getRating()+1.0f);}}classRatingBarListenerimplementsOnRatingBarChangeListener{@OverridepublicvoidonRatingChanged(RatingBarratingBar,floatrating,booleanfromUser){System.out.println("rating:"+rating+",fromUser:"+fromUser);Toast.makeText(MainActivity.this,"rating:"+rating+",fromUser:"+fromUser,Toast.LENGTH_SHORT).show();}}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。