Summary of trivial knowledge points in Android programming (1)

Source: Internet
Author: User

1. Get the color in the configuration file and set the control to this color.

Java code
  1. // Obtain the color in the color. xml file.
  2. Int TMP = getresources (). getcolor (R. drawable. Blue); // obtain the color in the configuration file.
  3. Mybutton. settextcolor (TMP );

2. Set the text font in the control.

Java code
  1. Mytext. settypeface (typeface. createfromasset (getassets (), "fonts/handmadetypewriter. TTF"); // set the font
  2. Fontbutton. settypeface (typeface. defaultfromstyle (typeface. italic); // you can set this parameter internally.
  3. // Note: 1. Ensure that the file must be in TTF format;
  4. // 2. Put it in the assets/fonts directory;
  5. // 3. If the corresponding font cannot be found, no error is reported, but it cannot be displayed during running.

3. Implement gesture and slide operations

3.1 first, implement their interfaces

Java code
  1. Public class gesturetest extends activity implements ontouchlistener, ongesturelistener {
  2. ....
  3. }

 

3.2 In the ontouch () method, we call the ontouchevent () method of gesturedetector and send the captured motionevent to gesturedetector to analyze whether there is a proper callback function to process user gestures.

Java code
  1. @ Override
  2. Public Boolean ontouch (view V, motionevent event ){
  3. // Ongesturelistener will analyzes the given motion event
  4. Return mygesturedetector. ontouchevent (event );
  5. }

3.3. Next, we have implemented the following six Abstract METHODS: onfling (), onscroll (), and onlongpress ().

Java code
  1. // The user (after touching the touch screen) is released and triggered by one motionevent action_up
  2. @ Override
  3. Public Boolean onsingletapup (motionevent e ){
  4. Return false;
  5. }
Java code
  1. // The user presses the touch screen and moves quickly before releasing it. It is triggered by one motionevent action_down, multiple action_move, and one action_up.
  2. @ Override
  3. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
  4. Return false;
  5. }
Java code
  1. // Press the touch screen and drag it. It is triggered by one motionevent action_down and multiple action_move operations.
  2. @ Override
  3. Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
  4. Return false;
  5. }
Java code
  1. // Remember to write it in the oncreate () method
  2. Mytextview. setontouchlistener (this );
  3. Mytextview. setlongclickable (true );

4. Use of SQLite in Android

4.1 sqlite3 supports five data types: NULL, integer, real (floating point number), text (string text), and blob (binary object). However, sqlite3 also accepts varchar (n), char (N), decimal (P, S) and other data types, but will be converted to the corresponding five data types during operation or storage. The biggest feature of SQLite is that you can save any type of data to any field, regardless of the Data Type declared in this column. However, fields defined as integer primary key can only store 64-bit integers.

4.2 SQLite can parse most standard SQL statements

SQL code
  1. Select * from Table Name
  2. Select * from table name order by ID DESC
  3. Select name from person group by name having count (*)> 1
  4. Insert into Table Name (Field List) values (Value List)
  5. Update table name set field name = value where Condition Clause
  6. Delete from table name where Condition Clause

4.3. The paging SQL statement is similar to MySQL. The following SQL statement obtains five records and skips the first three records.

SQL code
  1. Select * from account limit 5 offset 3 or select * from account limit 3, 5

4.4 sqlitedatabase is recommended:

The first parameter of the execsql (string SQL, object [] bindargs) method is the SQL statement, and the second parameter is the value of the placeholder parameter in the SQL statement, the order of parameter values in the array must correspond to the position of the placeholder.

SQL code
  1. Sqlitedatabase DB = ....;
  2. Db.exe csql ("insert into person (name, age) values (?,?) ", New object [] {" username ", 24 });
  3. DB. Close ();

4.5. rawquery () of sqlitedatabase is used to execute the SELECT statement. The first parameter of the rawquery () method is the SELECT statement, and the second parameter is the value of the placeholder parameter in the SELECT statement, if the SELECT statement does not use placeholders, this parameter can be set to null.

SQL code
  1. Sqlitedatabase DB = ....;
  2. Cursor cursor = dB. rawquery ("select * From person", null );
  3. While (cursor. movetonext ()){
  4. Int personid = cursor. getint (0); // obtain the value of the first column. The index of the first column starts from 0.
  5. String name = cursor. getstring (1); // obtain the value of the second column
  6. Int age = cursor. getint (2); // get the value of the third column
  7. }
  8. Cursor. Close ();
  9. DB. Close ();

4.6. Use transactions to operate SQLite Databases

Java code
  1. Sqlitedatabase DB = ....;
  2. DB. begintransaction (); // start the transaction
  3. Try {
  4. Db.exe csql ("insert into person (name, age) values (?,?) ", New object [] {" username ", 4 });
  5. Db.exe csql ("Update person set name =? Where personid =? ", New object [] {" zhangsan ", 1 });
  6. DB. settransactionsuccessful (); // call this method to submit the current transaction when it is executed to endtransaction (). If this method is not called, the transaction will be rolled back.
  7. } Finally {
  8. DB. endtransaction (); // The transaction identifier determines whether to commit or roll back the transaction.
  9. }
  10. DB. Close ();
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.