安卓天天练练(一)不讨巧的绝对布局

IDE 采用eclipse luna

使用了AbsoluteLayout标签后IDE立即开始警告:“AbsoluteLayout is deprecated”,不赞成使用绝对布局(无视它)

》需要注意

  • 如果看到报错"[Accessibility]Missing conetent description attribute on image"教学中也有出现,无视之
  • Graphical Layout的预览报java.lang错误却常常是xml标签未闭合,检查各标签>和/>
  • <Button />和<TextView />等等,无value内容的标签均可改写为自闭合
  • <EditText />可以从Graphical Layout工具的Palette中TextView分类任意可编辑文本框拖出来
  • <ScrollView></ScrollView>需要闭合标签
  • 手生敲xml注意区分android:@+id/和@id/
  • objname.setOnClickListener不会自动完成,但是在其内部的new View.OnClickListener()可以自动生成重写以及定义onClick函数
  • 熟练使用(tagname)findViewById(R.id.xxx)
  • 书写getText().toString()和setText().toString()

src/com.narumi.absolutelayout/MainActivity.java

 1 package com.narumi.absolutelayout;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.view.Menu;

 6 import android.view.MenuItem;

 7 import android.view.View;

 8 import android.widget.Button;

 9 import android.widget.EditText;

10 

11 public class MainActivity extends Activity {

12 

13     @Override

14     protected void onCreate(Bundle savedInstanceState) {

15         super.onCreate(savedInstanceState);

16         setContentView(R.layout.activity_main);

17         final Button okButton  = (Button) findViewById(R.id.btnok);

18         final Button cancelButton = (Button) findViewById(R.id.btcel);

19         final EditText uid = (EditText) findViewById(R.id.etuid);

20         final EditText pwd = (EditText) findViewById(R.id.etpwd);

21         final EditText log = (EditText) findViewById(R.id.etres);

22         okButton.setOnClickListener(

23                 new View.OnClickListener() {

24                     

25                     @Override

26                     public void onClick(View v) {

27                         // TODO Auto-generated method stub

28                         String uidStr = uid.getText().toString();

29                         String pwdStr = pwd.getText().toString();

30                         log.append("用户名:"+uidStr+"密码:"+pwdStr+"\n");

31                     }

32                 } );

33         

34         cancelButton.setOnClickListener(

35             new View.OnClickListener() {

36                 

37                 @Override

38                 public void onClick(View v) {

39                     // TODO Auto-generated method stub

40                     uid.setText("");

41                     pwd.setText("");

42                 }

43             } );

44     }

45 

46     @Override

47     public boolean onCreateOptionsMenu(Menu menu) {

48         // Inflate the menu; this adds items to the action bar if it is present.

49         getMenuInflater().inflate(R.menu.main, menu);

50         return true;

51     }

52 

53     @Override

54     public boolean onOptionsItemSelected(MenuItem item) {

55         // Handle action bar item clicks here. The action bar will

56         // automatically handle clicks on the Home/Up button, so long

57         // as you specify a parent activity in AndroidManifest.xml.

58         int id = item.getItemId();

59         if (id == R.id.action_settings) {

60             return true;

61         }

62         return super.onOptionsItemSelected(item);

63     }

64 }

下面两个自动生成的方法可以不要:

onCreateOptionsMenu

public boolean onOptionsItemSelected

编辑res/value中的string.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Android_6_3</string>

    <string name="uid">用户名</string>

    <string name="pvd">密码</string>

    <string name="ok">确定</string>

    <string name="cancel">取消</string>

</resources>

编辑res/layout中的activity_main.xml

 1 <AbsoluteLayout 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:paddingBottom="@dimen/activity_vertical_margin"

 6     android:paddingLeft="@dimen/activity_horizontal_margin"

 7     android:paddingRight="@dimen/activity_horizontal_margin"

 8     android:paddingTop="@dimen/activity_vertical_margin"

 9     tools:context="com.narumi.absolutelayout.MainActivity" >

10 

11     <TextView

12         android:id="@+id/tvuid"

13         android:layout_width="fill_parent"

14         android:layout_height="wrap_content"

15         android:text="@string/uid" />

16 

17     <TextView

18         android:id="@+id/tvpwd"

19         android:layout_width="fill_parent"

20         android:layout_height="wrap_content"

21         android:layout_x="21dp"

22         android:layout_y="61dp"

23         android:text="@string/pvd" />

24 

25     <EditText

26         android:id="@+id/etuid"

27         android:layout_width="167dp"

28         android:layout_height="wrap_content"

29         android:layout_x="61dp"

30         android:layout_y="-12dp"

31         android:ems="10"

32         android:inputType="text" >

33     </EditText>

34 

35     <EditText

36         android:id="@+id/etpwd"

37         android:layout_width="168dp"

38         android:layout_height="34dp"

39         android:layout_x="59dp"

40         android:layout_y="55dp"

41         android:ems="10"

42         android:inputType="numberPassword" >

43     </EditText>

44 

45     <ScrollView

46         android:id="@+id/scrollView1"

47         android:layout_width="wrap_content"

48         android:layout_height="wrap_content"

49         android:layout_x="48dp"

50         android:layout_y="144dp" >

51     </ScrollView>

52 

53     <EditText

54         android:id="@+id/etres"

55         android:layout_width="wrap_content"

56         android:layout_height="139dp"

57         android:layout_x="31dp"

58         android:layout_y="260dp"

59         android:ems="10"

60         android:inputType="text" >

61 

62     </EditText>

63 

64     <Button

65         android:id="@+id/btnok"

66         android:layout_width="wrap_content"

67         android:layout_height="wrap_content"

68         android:layout_x="72dp"

69         android:layout_y="175dp"

70         android:text="@string/ok" />

71 

72     <Button

73         android:id="@+id/btcel"

74         android:layout_width="wrap_content"

75         android:layout_height="wrap_content"

76         android:layout_x="165dp"

77         android:layout_y="175dp"

78         android:text="@string/cancel" />

79 

80 </AbsoluteLayout>

就这么简单,一练结束。

你可能感兴趣的:(安卓)