安卓第二课

一、针对特性配置

  1. 触控类型(Touchscreen type)
     notouch(无触控)
     finger(手指触控)
     stylus(触控笔触控)
  2. 文字输入方式(text input)
     nokeys (无实体键盘)
     qwerty(实体键盘)
     12key(数字键盘)
  3. 浏览方式(navigation method)
     notouch(无触控)
     dpad(方向键)
     trackball(轨迹球)
     wheel(滚轮)
  4. 配置文件中指定屏幕方向
     只要在Mainfest的activity中加上       android:screenOrientation="portrait",应用程序的屏幕就会将强制规定成为使用直式显示
     portrait:直式显示
     landscape:横式显示
     Sensor:自动切换
  5. 程序中指定屏幕方向
    要强制应用程序屏幕的显示方向,也可以在程序代码中添加:
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    SCREEN_ORIENTATION_LANDSCAPE:横式
    SCREEN_ORIENTATION_PORTRAIT:直式

 

 

二、信息提醒

public void onClick(View v) {
  EditText number_text = (EditText) this.findViewById(R.id.editText1);
  number = number_text.getText().toString();
  double weight = Double.parseDouble(number);
  if (weight > 25) {
   NotificationManager notManager = (NotificationManager) this
     .getSystemService(NOTIFICATION_SERVICE);
   Notification msg = new Notification(R.drawable.ic_launcher, "Hello!",
     System.currentTimeMillis());
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
     new Intent(MainActivity.this, OActivity.class),
     PendingIntent.FLAG_CANCEL_CURRENT);
   msg.setLatestEventInfo(this,"Welcom!", "This is Android ", contentIntent);
   notManager.notify(0, msg);
  }
 }

 

 

  • 主要架构:
       先声明一个“NotificationManager”类型的notManager对象,用来负责管理整个与这个Activity信息提醒相关的事物,然后在声明一个“Notification”类型的msg对象,用来放显示的信息内容,最后使用“NotificationManager”类型的“notify”方法将msg对象传递给Android框架,将提醒信息显示在状态栏上。
  • 分析:
  •  Notification msg = new Notification(R.drawable.ic_launcher, "Hello!",
         System.currentTimeMillis());
    第一个参数是要显示在屏幕上的图标资源,我们直接使用这个应用程序的图标,不管原来的图形资源大小如何, Android都会自动缩放成为适当大小,第二个参数是要马上显示在状态栏上的提醒信息,第三个参数是当这个“Notification”传送到的时间。

  •  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
         new Intent(MainActivity.this, OActivity.class),
         PendingIntent.FLAG_CANCEL_CURRENT)我们用到“PendingIntent”的“getActivity”方法,来在拥护迪娜选状态栏中这笔信息时,去执行一个Activity。

  •  msg.setLatestEventInfo(this,  "Welcom!", "This is Android ", contentIntent);
      第一个参数是传入当前的Activity对象以供参考,第二个参数是状态栏中显示的主题,第三个参数是状态栏中显示的说明, 第四个参数是接受“PendingIntent”类的传入参数,在这边指定当拥护按下状态栏中该笔信息的时候,我们可以让及其开始执行某些我们希望去执行的功能。
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  // 使用自定义图标
  menu.add(0, Menu.FIRST, 0, "关于").setIcon(R.drawable.ic_launcher);
  // 使用安卓内置的菜单图标
  menu.add(0, Menu.FIRST + 1, 0, "结束").setIcon(
    android.R.drawable.ic_menu_help);
  this.getMenuInflater().inflate(R.menu.test, menu);
  return super.onCreateOptionsMenu(menu);
}

 
              return true;跟 return super.onCreateOptionsMenu(menu);没有区别,跟我们平常做的在menu文件中添加也没有什么区别。Menu.FIRST是一个int类型的数据 ,所以可以用 int MENU_ABOUT = Menu.FIRST;int MENU_END= Menu.FIRST + 1;来代替。但是,我们把这段代码码上去,却发现图标显示不出来,这是为什么呢?这是因为我们目前用的Android的系统是4.4,而创建的Menu通过setIcon方法是没有办法给它添加图标的,这在2.3系统中可以显示出来。原因是涉及到菜单的源码类MenuBuilder发生了改变,mOptionalIconsVisible 的默认值为false,这个时候,有个非常简便又有效的方法,在AndroidManifest.xml中改变主题(Theme)为Theme就可以了!

三、布局
LinearLayout(线性布局)
线性布局是按照垂直或者水平的顺序来排列元素的。如果是垂直排列的话,元素排列就是N行一列,如果我们想在一行里面还添加好几个元素,我们就可以在里面再加上一个水平排列的线性布局。
我们可以利用  android:layout_weight="1"这一语句来改变组件在每一行中所占的比例,当一行只有一个元素时,默认为0。

   

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:text="@string/name" />

        <EditText
            android:singleLine="true"
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:ems="10"
            android:gravity="right"
            android:hint="@string/hint1"
            android:inputType="number"
            android:textColor="#550000"
            android:textSize="24dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/password" />

        <EditText
            android:id="@+id/password_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:ems="10"
            android:inputType="numberPassword" >

            <requestFocus />
        </EditText>
    </LinearLayout>

 
FrameLayout(单帧布局)
单帧布局中,我们不可以设置组件的位置,都是放在左上角,所以就会造成一层层叠加的情况。

AbsoluteLayout(绝对布局)
绝对布局,组件的位置都是由坐标来表示的,左上角为(0,0),往右往下坐标增大。


TableLayout(表格布局)
一个TableLayout是由多个TableRow组成的,每一个TableRow都是横向排列,宽高一致的。如果我们想要两行,我们就添加两个TableRow就可以了。

 <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

 
RelativeLayout(相对布局)
相对布局是我们用得最多的布局,就不详细解释了。


四、布局属性
接下来我们介绍一下我们用得比较多的布局属性
            android:hint="@string/hint1"是用来写EditText里面内容为空时的提示内容
            android:layout_gravity="bottom" 设置组件靠左靠右等
            android:gravity="right"设置view内容的靠左靠右
            android:textColor="#550000"设置text的颜色
            android:textSize="24dp"设置text的字体大小
            android:singleLine="true"设置text为单行显示
            android:cursorVisible="false"设置光标是否显示
            android:editable="false"设置是否可以编辑
            android:digits="2314144"设置允许输入哪些字符
            android:ellipsize="start"当内容过长时,该如何显示。start是开始有省略号,end是末尾有省略号,middle是中间有省略号,marquee是动画跑马灯显示。


             我们在EditText这样的组件中,可以看到系统初始设定的这样一句话,android:ems="10",意思是组件的长度等于10个字符的长度,这个长度我们当然是可以改变的。

五、生命周期
我们通过重写方法来进行测试,我们首先打开应用程序,点击按钮,到达第二个界面,在点返回键,回到第一个界面。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
 private String number;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button bt_test = (Button) this.findViewById(R.id.button1);
  bt_test.setOnClickListener(this);
  System.out.println("MainActivityonCreate");
 }

 public void onClick(View v) {
  Intent intent = new Intent();
  intent.setClass(MainActivity.this, O2Activity.class);
  this.startActivity(intent);
 }

 protected void onStop() {
  System.out.println("MainActivityonStop");
  super.onStop();
 }

 protected void onPause() {
  super.onPause();
  System.out.println("MainActivityonPause");
 }

 protected void onResume() {
  super.onResume();
  System.out.println("MainActivityonResume");
 }

 protected void onStart() {
  super.onStart();
  System.out.println("MainActivityonStart");
 }

 protected void onRestart() {
  super.onRestart();
  System.out.println("MainActivityonRestart");
 }

 protected void onDestroy() {
  super.onDestroy();
  System.out.println("MainActivityonDestroy");
 }
}

 
import android.os.Bundle;
import android.app.Activity;

public class O2Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_o2);
  System.out.println("O2ActivityonCreate");
 }

 protected void onStop() {
  System.out.println("O2ActivityonStop");
  super.onStop();
 }

 protected void onPause() {
  super.onPause();
  System.out.println("O2ActivityonPause");
 }

 protected void onResume() {
  super.onResume();
  System.out.println("O2ActivityonResume");
 }

 protected void onStart() {
  super.onStart();
  System.out.println("O2ActivityonStart");
 }

 protected void onRestart() {
  super.onRestart();
  System.out.println("O2ActivityonRestart");
 }

 protected void onDestroy() {
  super.onDestroy();
  System.out.println("O2ActivityonDestroy");
 }
}

下面是运行的结果:

MainActivityonCreate
MainActivityonStart
MainActivityonResume
MainActivityonPause
O2ActivityonCreate
O2ActivityonStart
O2ActivityonResume
MainActivityonStop
O2ActivityonPause
MainActivityonRestart
MainActivityonStart
MainActivityonResume
O2ActivityonStop
O2ActivityonDestroy

一、针对特性配置 触控类型(Touchscreen type)
 notouch(无触控)
 finger(手指触控)
 stylus(触控笔触控) 文字输入方式(text input)
 nokeys (无实体键盘)
 qwerty(实体键盘)
 12key(数字键盘) 浏览方式(navigation method)
 notouch(无触控)
 dpad(方向键)
 trackball(轨迹球)
 wheel(滚轮) 配置文件中指定屏幕方向
 只要在Mainfest的activity中加上       android:screenOrientation="portrait",应用程序的屏幕就会将强制规定成为使用直式显示
 portrait:直式显示
 landscape:横式显示
 Sensor:自动切换 程序中指定屏幕方向
要强制应用程序屏幕的显示方向,也可以在程序代码中添加:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
SCREEN_ORIENTATION_LANDSCAPE:横式
SCREEN_ORIENTATION_PORTRAIT:直式     二、信息提醒
public void onClick(View v) {
  EditText number_text = (EditText) this.findViewById(R.id.editText1);
  number = number_text.getText().toString();
  double weight = Double.parseDouble(number);
  if (weight > 25) {
   NotificationManager notManager = (NotificationManager) this
     .getSystemService(NOTIFICATION_SERVICE);
   Notification msg = new Notification(R.drawable.ic_launcher, "Hello!",
     System.currentTimeMillis());
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
     new Intent(MainActivity.this, OActivity.class),
     PendingIntent.FLAG_CANCEL_CURRENT);
   msg.setLatestEventInfo(this,"Welcom!", "This is Android ", contentIntent);
   notManager.notify(0, msg);
  }
 }
   
  • 主要架构:
       先声明一个“NotificationManager”类型的notManager对象,用来负责管理整个与这个Activity信息提醒相关的事物,然后在声明一个“Notification”类型的msg对象,用来放显示的信息内容,最后使用“NotificationManager”类型的“notify”方法将msg对象传递给Android框架,将提醒信息显示在状态栏上。
  • 分析:
  •  Notification msg = new Notification(R.drawable.ic_launcher, "Hello!",
         System.currentTimeMillis());
    第一个参数是要显示在屏幕上的图标资源,我们直接使用这个应用程序的图标,不管原来的图形资源大小如何, Android都会自动缩放成为适当大小,第二个参数是要马上显示在状态栏上的提醒信息,第三个参数是当这个“Notification”传送到的时间。

  •  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
         new Intent(MainActivity.this, OActivity.class),
         PendingIntent.FLAG_CANCEL_CURRENT)我们用到“PendingIntent”的“getActivity”方法,来在拥护迪娜选状态栏中这笔信息时,去执行一个Activity。

  •  msg.setLatestEventInfo(this,  "Welcom!", "This is Android ", contentIntent);
      第一个参数是传入当前的Activity对象以供参考,第二个参数是状态栏中显示的主题,第三个参数是状态栏中显示的说明, 第四个参数是接受“PendingIntent”类的传入参数,在这边指定当拥护按下状态栏中该笔信息的时候,我们可以让及其开始执行某些我们希望去执行的功能。

猜你喜欢

转载自501751420.iteye.com/blog/2096798