Activity启动流程
Android操作系统 ---> AndroidManifest.xml --->MainAcitivity.onCreate() --->activity_main.xml ...
sp 字体大小会随系统设置的改变而变
dp 字体大小不会随系统设置的改变而变
Ctrl+shift+O 自动导入
Fragment的知识特别重要
<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:background="#440000"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="20dp"
android:background="#ff0000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="first"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="second"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ff0000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="first"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="second"/>
</LinearLayout>
</LinearLayout>
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="first" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:text="second" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ff00"
android:text="first" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#0000ff"
android:layout_weight="2"
android:text="second" />
</LinearLayout>
</LinearLayout>
<RelativeLayout 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:padding="15dp"
>
<TextView
android:id="@+id/lableView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="登陆界面" />
<EditText
android: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"/>
<EditText
android: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"/>
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/passwordText"
android:layout_alignParentRight="true"
android:text="取消"
/>
<Button
android: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>
<AnalogClock
android:id="@+id/clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<DatePicker
android:id="@+id/firstDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstDatePicker"
android:text="获取DatePicker的值"/>
public class MainActivity extends Activity {
private DatePicker datePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
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);
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
int y = datePicker.getYear();
int m = datePicker.getMonth();
int d = 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();
}
}
}
<TimePicker
android:id="@+id/firstTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstTimePicker"
android:text="获取TimePicker的值"
/>
public class MainActivity extends Activity {
private TimePicker firstTimePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstTimePicker = (TimePicker)findViewById(R.id.firstTimePicker);
button = (Button)findViewById(R.id.button);
//该函数用于设置是否使用24小时制显示时间
firstTimePicker.setIs24HourView(true);
TimeListener timeListenter = new TimeListener();
firstTimePicker.setOnTimeChangedListener(timeListenter);
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
int hour = firstTimePicker.getCurrentHour();
int minute = firstTimePicker.getCurrentMinute();
Toast.makeText(MainActivity.this, "h:" + hour + ",minute:" + minute, Toast.LENGTH_SHORT).show();
}
}
class TimeListener implements OnTimeChangedListener{
/**
* view:该对象代表着TimePicker
* hourOfDay:用户所选择的小时
* minute:用户所选择的分钟
*/
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
System.out.println("Hour:" + hourOfDay + ",minute:" + minute);
}
}
}
<ProgressBar
android:id="@+id/firstProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/firstButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstProgressBar"
android:text="增加第一进度"/>
<Button
android:id="@+id/secondButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstButton"
android:text="增加第二进度"/>
public class MainActivity extends Activity {
private ProgressBar progressBar;
private Button firstButton;
private Button secondButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
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(new FirstListener());
secondButton.setOnClickListener(new SecondListener());
}
class FirstListener implements OnClickListener{
@Override
public void onClick(View v) {
progressBar.incrementProgressBy(10);
}
}
class SecondListener implements OnClickListener{
@Override
public void onClick(View v) {
progressBar.incrementSecondaryProgressBy(20);
}
}
}
<SeekBar
android:id="@+id/firstSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
/>
public class MainActivity extends Activity {
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar)findViewById(R.id.firstSeekBar);
seekBar.setProgress(30);
seekBar.setSecondaryProgress(50);
SeekBarListener listener = new SeekBarListener();
seekBar.setOnSeekBarChangeListener(listener);
}
class SeekBarListener implements OnSeekBarChangeListener{
/**
* seekBar 该对象指的是触发了监听器的SeekBar对象
* progress 指的是当前SeekBar的进度
* fromUser
*/
@Override
public void onProgressChanged(SeekBar SeekBar, int progress, boolean fromUser) {
System.out.println("progress:" + progress + ",fromUser:" + fromUser);
Toast.makeText(MainActivity.this, "progress:" + progress + ",fromUser:" + fromUser, Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println("onStart");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
System.out.println("onStop");
}
}
}
<RatingBar
android:id="@+id/firstRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="4"
android:stepSize="1"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firstRatingBar"
android:text="button"/>
public class MainActivity extends Activity {
private RatingBar ratingBar;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar)findViewById(R.id.firstRatingBar);
button = (Button)findViewById(R.id.button);
RatingBarListener listener = new RatingBarListener();
ratingBar.setOnRatingBarChangeListener(listener);
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
ratingBar.setRating(ratingBar.getRating() + 1.0f);
}
}
class RatingBarListener implements OnRatingBarChangeListener{
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
System.out.println("rating:" + rating + ",fromUser:" + fromUser);
Toast.makeText(MainActivity.this, "rating:" + rating + ",fromUser:" + fromUser, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。