當前位置:首頁 » 安卓系統 » android學習手冊

android學習手冊

發布時間: 2022-02-27 05:11:12

Ⅰ android彈窗效果怎麼搞

使用AlertDialog
下面解決辦法來自於android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載。排到第三個。
AlertDialog的構造方法全部是Protected的,所以不能直接通過new一個AlertDialog來創建出一個AlertDialog。
要創建一個AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder創建對話框需要了解以下幾個方法:
setTitle :為對話框設置標題setIcon :為對話框設置圖標setMessage:為對話框設置內容setView : 給對話框設置自定義樣式setItems :設置對話框要顯示的一個list,一般用於顯示幾個命令時setMultiChoiceItems :用來設置對話框顯示一系列的復選框setNeutralButton :普通按鈕
setPositiveButton :給對話框添加"Yes"按鈕
setNegativeButton :對話框添加"No"按鈕
create : 創建對話框
show :顯示對話框
一、簡單的AlertDialog
下面,創建一個簡單的ALertDialog並顯示它:

[java] view plain print?
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對話框的標題").
setMessage("對話框的內容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}

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

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對話框的標題").
setMessage("對話框的內容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}

運行結果如下:

二、帶按鈕的AlertDialog

上面的例子很簡單,下面我們在這個AlertDialog上面加幾個Button,實現刪除操作的提示對話框
[java] view plain print?
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}

運行結果如下:

Ⅱ 誰能推薦好的android進階書籍

組件是在需要的時候才會使用,
比如servie ,了解注意它的生命周期。和什麼情況下使用,運行在系統中,什麼時候殺掉,什麼時候不讓它被殺掉。等等。
弄安卓有小一段時間了,控制項。組件,線程,非同步都不難用,主要是有編程思想,
可惜我現在還沒有。腦子比較亂。不用刻意去弄這些。做項目的時候需要的時候,
網路,看下API 很簡單。
做完一個客戶端。基本都涉及了。
也算是了解了。
書籍的話,瘋狂的講義 不錯。

Ⅲ 用android studio遇到一個問題(ret":0,) 我怎麼才能做到取"0"這個呢左邊是ret":符號和字母,右邊是,

共有n種方式。

1 如果這個字元串是以ret":開頭,以,結尾,直接用

Stringstrtt="ret":0,";
Stringstr5=strtt.substring(5,strtt.length()-1);
System.out.println(str5);

2 最好的方式,正則表達式。

下面是個代碼來自於android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼

Stringstrtt="ret":0,";
PatternpatternT=Pattern.compile("ret":(.*?),");
Matchermatcher3=patternT.matcher(strtt);
while(matcher3.find()){
Stringstr2=strtt.substring(matcher3.start()+5,matcher3.end()-1);
//System.out.println("Valueis:"+matcher3.group(1)+""+matcher3.start()+""+matcher3.end());
System.out.println(str2);


}

Ⅳ 學習編程是看java學習手冊有用還是Android第一行代碼有用

如果你是為了學習安卓而學習java,那麼你只需要看看java相關的視頻就可以了,比如尚學堂馬士兵老師的java教程。然後再看一遍第一行代碼這本書,這本書通俗易懂,很適合初學者,看完之後照著練習。然後就可以看一些android案例之類的書籍,學習寫項目了。第一行代碼的都是很基礎的東西,要做項目其實很多時候都是要用到開源庫的,比如請求網路數據,真正項目開發中就很少自己封裝HttpClient或者HttpUrlConnection了,類似獲取新聞客戶端這種很多新聞信息,就可以用google推出的框架volley。使用volley框架後,網路數據的請求,特別是圖片的請求和緩存就變得非常容易了。

Ⅳ android怎麼使用sqlopenhelper

下面是SQLiteOpenHelper增刪改查的例子。

例子來自於android學習手冊,裡面有源碼。android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼



我們大家都知道Android平台提供給我們一個資料庫輔助類來創建或打開資料庫,這個輔助類繼承自SQLiteOpenHelper類,在該類的構造器中,調用Context中的方法創建並打開一個指定名稱的資料庫對象。繼承和擴展SQLiteOpenHelper類主要做的工作就是重寫以下兩個方法。

onCreate(SQLiteDatabase db) : 當資料庫被首次創建時執行該方法,一般將創建表等初始化操作在該方法中執行。
onUpgrade(SQLiteDatabse dv, int oldVersion,int new Version):當打開資料庫時傳入的版本號與當前的版本號不同時會調用該方法。

除了上述兩個必須要實現的方法外,還可以選擇性地實現onOpen 方法,該方法會在每次打開資料庫時被調用。

SQLiteOpenHelper 類的基本用法是:當需要創建或打開一個資料庫並獲得資料庫對象時,首先根據指定的文件名創建一個輔助對象,然後調用該對象的getWritableDatabase 或 getReadableDatabase方法 獲得SQLiteDatabase 對象。

調用getReadableDatabase 方法返回的並不總是只讀資料庫對象,一般來說該方法和getWriteableDatabase 方法的返回情況相同,只有在資料庫僅開放只讀許可權或磁碟已滿時才會返回一個只讀的資料庫對象。

下面通過一個簡單的小例子說明SQLiteOpenDatabase的用法,其中包括創建資料庫、插入數據、更新 、查詢等等,我們將查詢後獲取到的數據顯示到TextView上,看一下運行後的效果。MySQLiteHelper

Java代碼

packagexiaohang.meng;

importandroid.content.Context;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.database.sqlite.SQLiteOpenHelper;

importandroid.database.sqlite.SQLiteDatabase.CursorFactory;

{

//調用父類構造器

publicMySQLiteHelper(Contextcontext,Stringname,CursorFactoryfactory,

intversion){

super(context,name,factory,version);

}

/**

*當資料庫首次創建時執行該方法,一般將創建表等初始化操作放在該方法中執行.

*重寫onCreate方法,調用execSQL方法創建表

**/

@Override

publicvoidonCreate(SQLiteDatabasedb){

db.execSQL("createtableifnotexistshero_info("

+"idintegerprimarykey,"

+"namevarchar,"

+"levelinteger)");

}

//當打開資料庫時傳入的版本號與當前的版本號不同時會調用該方法

@Override

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){

}

}



Activity01

Java代碼

packagexiaohang.meng;

importandroid.app.Activity;

importandroid.content.ContentValues;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.graphics.Color;

importandroid.os.Bundle;

importandroid.widget.TextView;

{

MySQLiteHelpermyHelper;

TextViewtv;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv=(TextView)findViewById(R.id.tv);

//創建MySQLiteOpenHelper輔助類對象

myHelper=newMySQLiteHelper(this,"my.db",null,1);

//向資料庫中插入和更新數據

insertAndUpdateData(myHelper);

//查詢數據

Stringresult=queryData(myHelper);

tv.setTextColor(Color.RED);

tv.setTextSize(20.0f);

tv.setText("名字 等級 "+result);

}

//向資料庫中插入和更新數據

publicvoidinsertAndUpdateData(MySQLiteHelpermyHelper){

//獲取資料庫對象

SQLiteDatabasedb=myHelper.getWritableDatabase();

//使用execSQL方法向表中插入數據

db.execSQL("insertintohero_info(name,level)values('bb',0)");

//使用insert方法向表中插入數據

ContentValuesvalues=newContentValues();

values.put("name","xh");

values.put("level",5);

//調用方法插入數據

db.insert("hero_info","id",values);

//使用update方法更新表中的數據

//清空ContentValues對象

values.clear();

values.put("name","xh");

values.put("level",10);

//更新xh的level為10

db.update("hero_info",values,"level=5",null);

//關閉SQLiteDatabase對象

db.close();

}

//從資料庫中查詢數據

publicStringqueryData(MySQLiteHelpermyHelper){

Stringresult="";

//獲得資料庫對象

SQLiteDatabasedb=myHelper.getReadableDatabase();

//查詢表中的數據

Cursorcursor=db.query("hero_info",null,null,null,null,null,"idasc");

//獲取name列的索引

intnameIndex=cursor.getColumnIndex("name");

//獲取level列的索引

intlevelIndex=cursor.getColumnIndex("level");

for(cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()){

result=result+cursor.getString(nameIndex)+" ";

result=result+cursor.getInt(levelIndex)+" ";

}

cursor.close();//關閉結果集

db.close();//關閉資料庫對象

returnresult;

}

@Override

protectedvoidonDestroy(){

SQLiteDatabasedb=myHelper.getWritableDatabase();//獲取資料庫對象

//刪除hero_info表中所有的數據傳入1表示刪除所有行------>點擊back按鈕

db.delete("hero_info","1",null);

super.onDestroy();

}

}

Ⅵ 新手開發android APP 直接給個介面怎麼開發

不需要。就是使用http協議,

下面是使用截圖,看網路部分

//和GET方式一樣,先將參數放入List
params=newLinkedList<BasicNameValuePair>();
params.add(newBasicNameValuePair("param1","Post方法"));
params.add(newBasicNameValuePair("param2","第二個參數"));

try{
HttpPostpostMethod=newHttpPost(baseUrl);
postMethod.setEntity(newUrlEncodedFormEntity(params,"utf-8"));//將參數填入POSTEntity中

HttpResponseresponse=httpClient.execute(postMethod);//執行POST方法
Log.i(TAG,"resCode="+response.getStatusLine().getStatusCode());//獲取響應碼
Log.i(TAG,"result="+EntityUtils.toString(response.getEntity(),"utf-8"));//獲取響應內容

}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClientProtocolExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

Ⅶ 新手Android中px=dp*(dpi/160)的解釋

在160dpi的手機上1px=1dp,這句話的以上是說,手機屏幕寬頻被分割成了160塊,每一塊有一個像素點,每一塊就是一個dp。 那麼1dp=1px

而320dpi的手機上,解析度是上一個手機的兩倍,手機屏幕寬頻也被分割成了160塊,每一塊有兩個像素點,每一塊也是一個dp,那麼1dp=2px

上面分析來自於android學習手冊,360手機助手中可以下載,裡面有108個android例子,源碼文檔都可在裡面看,下面是截圖

importandroid.content.Context;

publicclassDensityUtil{

/**
*根據手機的解析度從dp的單位轉成為px(像素)
*/
publicstaticintdip2px(Contextcontext,floatdpValue){
finalfloatscale=context.getResources().getDisplayMetrics().density;
return(int)(dpValue*scale+0.5f);
}

/**
*根據手機的解析度從px(像素)的單位轉成為dp
*/
publicstaticintpx2dip(Contextcontext,floatpxValue){
finalfloatscale=context.getResources().getDisplayMetrics().density;
return(int)(pxValue/scale+0.5f);
}
}

Ⅷ 想看一個開源的Android Studio 項目 , 新手看不懂 有沒有大神教教我該怎麼看

看開源的項目的,一定要看他的開發文檔,有很多注釋

Ⅸ 自學android開發需要學習哪些課程

自己多模仿一些開源代碼寫demo就好了,比較著名的開源項目有小米便簽等,你可以搜搜。

Ⅹ android 多個頁面怎麼共用同一組件

自定義組件就行。如果覺得還不好就創建一個mole。

關於自定義組件的實例,請參考android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼

.多個Activity共用相同布局或者相同控制項:避免重復代碼;

BaseActivit代碼:

{

/*

* 多個Activity共用相同布局或者相同控制項

* 寫個基類繼承,避免重復代碼

*/

@Override

publicvoidonClick(View v)

{

switch(v.getId())

{

caseR.id.common_titlebar_btn_back:

finish();

break;

default:

break;

}

baseOnClick(v);

}

protectedvoidbaseOnClick(View v)

{

}

@Override

protectedvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

// setContentView(R.layout.activity_base);

}

@Override

protectedvoidonResume()

{

super.onResume();

onPostOnCreate();

}

publicfinalvoidonPostOnCreate()//find同一個控制項

{

findViewById(R.id.common_titlebar_btn_back).setOnClickListener(this);

}

@Override

(Menu menu)

{

getMenuInflater().inflate(R.menu.base, menu);

returntrue;

}

}


子類代碼:


{

/**

* layout里復用同一布局:

* < include

* android:id="@+id/include1"

* layout="@layout/common_titlebar" />

*/

@Override

protectedvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_third_main);

}

}

熱點內容
android路線規劃 發布:2025-03-14 13:23:22 瀏覽:302
poi瀏覽器島風go緩存 發布:2025-03-14 13:10:24 瀏覽:187
具體可要說存儲在鋼瓶中是因為 發布:2025-03-14 13:00:36 瀏覽:440
汽車空調壓縮機不轉了 發布:2025-03-14 12:55:45 瀏覽:30
安卓和平營地cp怎麼組 發布:2025-03-14 12:55:40 瀏覽:604
時序模式演算法 發布:2025-03-14 12:50:45 瀏覽:203
爐石傳說標准模式多腳本 發布:2025-03-14 12:47:53 瀏覽:210
密碼鎖用密碼打不開是什麼原因 發布:2025-03-14 12:31:25 瀏覽:196
低溫存儲測試 發布:2025-03-14 12:10:22 瀏覽:245
c語言二維數組的輸出 發布:2025-03-14 11:58:10 瀏覽:26