Android综合下载系统的开发步骤以及遇到问题

来源:互联网 发布:mp3播放器推荐 知乎 编辑:程序博客网 时间:2024/06/10 14:30


 

1.设置title为”android综合下载测试系统”。

2.android:gravity="center",将TextView中的字体设置为居中。

3. “Error in an XML file: aborting build.”,出现该问题,使用project->clean消除bug

4.使用res/values/color.xml,这样可以使用android:background=”@drawable/white”

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

<resources>

       <drawablename="darkgray">#808080FF</drawable>

       <drawablename="white">#FFFFFFFF</drawable>

</resources>

5.中途出现了一些小插曲,如果MyEclipse无法打开时,

The quickest wayto fix this or any other resource-related exception is to:

Shut down EclipseIDE

Remove and backupyour workspace/.metadata/.plugins/org.eclipse.core.resources directory

Start Eclipse IDE(with -clean to be super-safe)

Reimport allprojects (UPDATE: Just use File->Import->Existing Project into Workspaceand browse your workspace/project directory)

Enjoy

6.android:inputType="textPassword"将输入框设置为密码格式

7.生成一个checkbox,注意加粗体,否则报错

  <CheckBox

         android:id="@+id/display"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="@string/display"

         />

8.在main.xml中嵌入一个relativeLayout

  <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android

 

9.使用相对布局将loginButton放在searchButton的左边

  android:layout_toRightOf="@id/buttonSearch"

10.设置密码可以显示不可以显示代码:

if(display.isChecked()){

                                               //将密码显示出来

                                               //myPassword控件进行设置TransformationMethod                    myPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

                                     }else{

                                               //不显示密码

         myPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

                                     }

11.进行登陆操作

  密码错误时:使用Toast方法来显示信息

  Toast.makeText(Login.this, "密码错误!" , Toast.LENGTH_LONG).show();

  密码正确时,使用Intent方法进入下一个Activity

12.在新的一个Activity中创建布局描述文件.xml,注意:android:orientation="vertical"

13.添加RadioGroup以及RadioButton

   <RadioGroup

         android:id="@+id/typeGroup"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:orientation="vertical"

         >

         <RadioButton

                   android:id="@+id/xml"

                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"

                   android:text="@string/xml"

                   />

14.为RadioGroup添加OnCheckedListener监听器

//为RadioGroup添加监听器

                   typeGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {

                           

                            @Override

                            public voidonCheckedChanged(RadioGroup group, int checkedId) {

                                     // TODOAuto-generated method stub

                                     if(xml.getId()== checkedId){

                                               flag= 1;

                                     }elseif(txt.getId() == checkedId){

                                               flag= 2;

                                     }elseif(mp3.getId() == checkedId){

                                               flag= 3;

                                     }

                                    

                            }

                   });

15.开始使用SQLite进行登陆操作

 1.使用SQLiteOpenHelper类进行创建数据库:

  public class DatabaseHelper extendsSQLiteOpenHelper {

          publicDatabaseHelper(Context context, String name, CursorFactory factory,

                            int version) {

                   super(context, name, factory,version);

                   // TODO Auto-generatedconstructor stub

         }

    @Override

         public void onCreate(SQLiteDatabase db){

                   // TODO Auto-generated methodstub

                  

                  

                   String sql = "createtable user(username varchar(20),password varchar(20))";

                   db.execSQL(sql);

                   System.out.println("createa database");

         }

}

 

2.在监听器中进行操作:

查询:

DatabaseHelper dbHelper = newDatabaseHelper(Login.this, "userlist");

                                 SQLiteDatabasedb = dbHelper.getReadableDatabase();

                               Cursorcursor = db.query("user", newString[]{"username","password"}, "username=?",new String[]{tempUser}, null, null, null);

                                 while(cursor.moveToNext()){

                                          password= cursor.getString(cursor.getColumnIndex("password"));

                                 }

插入删除操作:

ContentValues values = new ContentValues();

values.put("username",usernameString);

values.put("password",passwordString);

DatabaseHelper dbHelper = newDatabaseHelper(Register.this, "userlist");

SQLiteDatabase db =dbHelper.getWritableDatabase();

db.insert("user", null, values);

 

 

 

 

 

 

 

16.进行文件下载:

1.对URL创建对象,并传回inputStream:

  publicInputStream getInputStreamFromUrl(String urlStr)

                       throwsMalformedURLException, IOException {

              url= new URL(urlStr);

     //使用URLConnection打开connection

              HttpURLConnectionurlConn = (HttpURLConnection) url.openConnection();

    //使用URLConnection得到InuptStream

              InputStreaminputStream = urlConn.getInputStream();

              returninputStream;

     }

2.对于文本文件:使用IO流读取数据:

  buffer= new BufferedReader(new InputStreamReader(urlConn

                                          .getInputStream()));

                       while((line = buffer.readLine()) != null) {

                                 sb.append(line);

                       }

3.对于SDCARD,必须对SDCARD进行操作,使用代码,对SDCARD进行写操作:

   Filefile = null;

              OutputStreamoutput = null;

              try{

                       creatSDDir(path);

                       file= creatSDFile(path + fileName);

                       output= new FileOutputStream(file);

                       bytebuffer [] = new byte[4 * 1024];

                       while((input.read(buffer))!= -1){

                                 output.write(buffer);

                       }

                       output.flush();

              }

4.关键:

  SDPATH= Environment.getExternalStorageDirectory() + "/";

得到SDPATH,发现SD卡的路径

<uses-permissionandroid:name="android.permission.INTERNET"/>

 <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  使程序访问网络并且具备SD卡的读写权限

5.回顾了2个Activity之间的数据传输问题:

 bundle.putString("textDownload",text);

  intent.putExtras(bundle);

  startActivity(intent);

17.XML解析:

  1.对XML字符串进行解析

  //先创建一个SAXparseFactory

SAXParserFactoryfactory = SAXParserFactory.newInstance();

//获取到XMLReader对象

XMLReader reader =factory.newSAXParser().getXMLReader();

//设置解析适配器

reader.setContentHandler(newMyContentHandler());

//进行解析

reader.parse(newInputSource(new StringReader(text)));

2.创建MyContentHandler类继承DefaultHandler(ContentHandler)

  重写5个方法

  System.out.println(attributes.getLocalName(i)+ "=" + attributes.getValue(i));

  if(tagName.equals("name")){

                            hisName = newString(ch, start, length);

                   }elseif(tagName.equals("sex")){

                            hisSex = newString(ch, start, length);

                   }

原创粉丝点击