Android 中的五種布局
1,LinearLayout(線性布局),RelativeLayout(相對布局),F(xiàn)rameLayout(幀布局),AbsoluteLayout(絕對布局),TableLayout(表格布局)。
2,線性布局中的控件屬性說明
①android:background,設(shè)置UI控件的背景,其值可以是資源文件夾中的圖片或者是顏色的十六進(jìn)制值。
②android:orientation,該屬性是線性布局的特有屬性,用來指定線性布局的方向,vertical表示垂直線性布局,horizontal表示水平線性布局
③android:layout_width:設(shè)置UI控件的寬度,match_parent的含義是設(shè)置UI控件的寬度充滿父窗體,wrap_content的含義是設(shè)置UI控件的寬度隨內(nèi)容的大小變化
④android:id,該屬性為UI控件設(shè)置一個名字,在源代碼程序中可以通過R.id名字的形式來獲取這個控件實體
⑤android:src,用來設(shè)置圖片控件ImmageView的前景圖片
⑥android:text,設(shè)置UI控件上面顯示的文字內(nèi)容。
android:textSize,設(shè)置控件中文字的大小。最好用sp為單位,控件寬度和高度多采用dip做單位
⑦android:textColor,設(shè)置字體的顏色,采用八位十六進(jìn)制數(shù)格式為該屬性設(shè)置值。
⑧android:gravity,設(shè)置控件在父容器中的對齊方式,其值可以是水平居中,垂直居中,靠左,靠右
3,相對布局控件屬性說明
①android:layout_alignParentBottom,設(shè)置UI控件貼緊父元素底部,如需要與父元素的底部對齊,將其值設(shè)為true,否則為false。
②android:layout_alignParentRight,設(shè)置UI控件緊貼父元素的右邊,其值設(shè)置同上屬性
③android:layout_centerHorizontal,設(shè)置控件在相對布局中水平居中
④android:layout_centerInParent,設(shè)置父控件相對于父元素完全居中
⑤android:layout_alignParentLeft,設(shè)置控件貼緊父元素的左邊緣
⑥android:layout_alignParentTop,設(shè)置控件貼緊父元素的上邊緣
以上屬性值均為true或false
android:layout_below,設(shè)置控件在某元素的下方
android:layout_above,設(shè)置控件在某元素的上方
android:layout_toRightOf,設(shè)置控件在某元素的右邊
android:layout_alignTop,設(shè)置控件本元素的上邊緣和某元素大的上邊緣對齊
以上屬性的屬性值必須為其他控件的id引用名。
4,幀布局中控件屬性說明
android:layout_gravity,該屬性用于設(shè)置當(dāng)前控件在父元素中的對齊方式,若其值為center_vertical,表示當(dāng)前控件在父元素中垂直居中,若其值為center_horizontal表示當(dāng)前控件在父元素中水平居中,若兩個值并列使用,則表示控件對父元素既垂直居中有水平居中。
android:layout_gravity=‘center_vertical|center_horizontal’
②android:visibility,設(shè)置控件的可見性,該屬性有三個值,visible,gone,invisible.visible代表控件可見,invisible代表控件不可見,但占據(jù)布局位置,gone帶代表不可見也而且不占據(jù)布局位置。
Android 中的UI控件
1,開發(fā)中的常用基礎(chǔ)控件包括TextView,Button,EditText,ImageView,RadioBuutton,CheckBox,ProgressBar。
2,文本控件TextView
3,編輯框控件EditText
4,按鈕控件Button
繼承自aandroid.widget.TextView類,位于android.widget包中。它常用的子類有CheckBox,RadioButton,ToggleButton.
其常用屬性為android:click 設(shè)置當(dāng)按鈕點擊時執(zhí)行的方法名
5,圖片控件ImageView
6,單選按鈕RadioButton
一般在RadioGroup中聲明,用于提供兩個或多個互斥選項,即在一組按鈕中只能選擇一個。要監(jiān)聽單選按鈕狀態(tài)的更改,需要給他所在的RadioGroup添加setOnCheckChangeListener()監(jiān)聽器。
7,進(jìn)度條控件ProgressBar
一般在應(yīng)用程序中用來顯示用戶某個耗時操作的進(jìn)度百分比
8,高級控件
常用的幾個高級控件都直接或間接地繼承了android.view.ViewGroup類。常用地高級控件主要包括ListView,GridView,Spinner,AutoCompleteTextView,ExpandableListView,Gallery等。
9,高級控件案例ListView控件是以列表地形式顯示資源,Spinner是以下拉列表地形式顯示數(shù)據(jù)資源,Grid View、是以網(wǎng)格地形式顯示數(shù)據(jù)資源。
①用戶單擊控件條目事件。為控件加載setOnTremClickListener監(jiān)聽,重寫onItemClick()方法
②用戶長按事件。為控件加載setOnItemLongClickListener監(jiān)聽,重寫onItemLongClick()方法。
1,ListView使用ArrayAdapter適配器
public class MyListView extends Activity{
String[] strs =new String[] {"first", "second", "third", "fourth","fifth"//定義一個String數(shù)組用來顯示ListView的內(nèi)容
private ListView lv;
@Override
publicvoid onCreate(Bundle savedInstanceState)}
super.onCreate(savedlnstanceState);
setContent View(R.layout.main);
//得到ListView對象的引用
lv = (ListView) findViewBy!d(R.id.Iv);
//為ListView設(shè)置Adapter來綁定數(shù)據(jù)
lv. set Adapter(ne w Array AdapterくString>(this,
android.R.layout.simple 一list一item j,strs));
}
}
Android 中的交互與菜單
1,Toast提示框的創(chuàng)建
只需要調(diào)用Toast類的靜態(tài)方法makeText即可創(chuàng)建默認(rèn)格式的Toast提示框。makeText()方法包含三個參數(shù),第一個參數(shù)指定上下文環(huán)境,一般使用getApplicationContext()方法獲取當(dāng)前程序的上下文,第二個參數(shù)是彈出的提示框中顯示的提示文本,第三個參數(shù)是Toast提示框顯示的時間,0,代表短時間,1代表長時間。
例:Toast.makeText(getApplicationContext(),“文本”,0).show();
//設(shè)置顯示的位置,該方法有三個參數(shù),第一個參數(shù)是顯示的位置,第二個參數(shù)是距離該位置的x方向的。
toast.setGravity(Gravity.LEFT,20,30); toast.show();
2.帶圖片的Toast
//定義一個Toast的對象
Toast toast=Toast.makeText(getApplicationContext(), ”圖片 toast", 0);
//定義一個ImageView的對象,構(gòu)造的時候需要上下文作為參數(shù)
ImageView imageView=new ImageView(MainActivity.this);
"設(shè)置ImageView的源
image View.setImageResource(R.drawable.ic」auncher);
//獲取當(dāng)前的Toast對象的View并轉(zhuǎn)換為線性布局
LinearLayout layout=(LinearLayout) toast.getView();
//ImageView添加到線性布局中
layout.addView(imageView);
"顯示Toast
toast.show();
3,常用的設(shè)置對話框的方法
setTitle:為對話框設(shè)置標(biāo)題
setIcon:為對話框設(shè)置圖標(biāo)
setMessage:為對話框設(shè)置內(nèi)容
setView:為對話框設(shè)置自定義樣式
setItem:設(shè)置對話框要顯示一個list,一般用于顯示幾個命令
setMultiChoiceItems:用來設(shè)置對話框顯示一系列的復(fù)選框
setNeutralBuutton:普通按鈕
setPositibeButton:一般用作確定按鈕
setNegativeButton:一般用作取消按鈕
setCancelable:設(shè)置是否模態(tài),如果為模態(tài),用戶必須進(jìn)行操作
creat:創(chuàng)建對話框
show:顯示對話框
5.2.2 創(chuàng)建對話框的關(guān)鍵代碼
1.警告對話框
//創(chuàng)建對話框的對象
AlertDialog.Builder builder=new Builder(context);
//設(shè)置對話框的標(biāo)題
builder.setTitle(“提示”);
//設(shè)置對話框的內(nèi)容
builder.setMessage(“是否要退出”);
//設(shè)置一個確定按鈕, 并使用匿名內(nèi)部類的方式創(chuàng)建點擊事件
builder.setPositiveButton(“確定”,new DialogInterface.OnClickListener(O{
public void onClick(DialogInterface dialog, int which){
//彈出 一個點擊確定的提示框
Toast.makeText(getApplicationContext), “點擊確定”,0).show();
}
});
//設(shè)置 一個取消按鈕, 并使用匿名內(nèi)部類的方式創(chuàng)建點擊事件
builder.setNegativeButton(“取消”, new DialogInterface.OnClickListenerO {
public void onClick(DialogInterface dialog, int which){
//彈出一個點擊取消的提示信息
Toast.makeText(getApplicationContext(),“點擊取消” ,1).showO;
1
});
//創(chuàng)建對話框并通過showO方法使其顯示出來
builder.create().show();
2. 進(jìn)度對話框
//創(chuàng)建 一個進(jìn)度對話框的對象
ProgressDialog pdd=new ProgressDialog(this);
//設(shè)置進(jìn)度對話框的進(jìn)度條為水平方向的對話框
pdd.setProgressStyle(ProgressDialog.STYLE_ HORIZONTAL);
//設(shè)置對話框的信息
pdd.setMessage(“正在下載。。。請稍候”);
//設(shè)置對話框的標(biāo)題
pdd.setTitle(“下載中”);
//設(shè)置對話框中進(jìn)度條的最大值
pdd.setMax(100); (用貨
//設(shè)置當(dāng)前的進(jìn)度
pdd.setProgress(40);
//顯示進(jìn)度對話框
pdd.show);
3. 日期選擇對話框
//創(chuàng)建一個日歷對象獲取當(dāng)前的日期
Calendar c=Calendar.getInstanceO);
//獲取日期中的年
int y=c.get(Calendar.YEAR);
11獲取日期中的月
int m=c.get(Calendar.MONTH);
//獲取日期中的天
int d=c.get(Calendar.DAY_OF_MONTH);
/*定義一個日期選擇對話框DatePickerDialog, 構(gòu)造方法需要五個參數(shù),第一個參數(shù)是上下文
環(huán)境, 第二個參數(shù)是日期變動的回調(diào)函數(shù), 就是當(dāng)對話框中的日期改變時所執(zhí)行的函數(shù), 第三個是
參數(shù)是默認(rèn)顯示的年, 第四個參數(shù)是默認(rèn)顯示的月, 第五個參數(shù)是默認(rèn)顯示的天
DatePickerDialog da=new DatePickerDialog(this,null,y,m,d);
//讓對話框顯示出來 I
請輸入名字
da.show);
4.自定義對話框
創(chuàng)建自定義對話框需要自定義的布局文件,如圖5-2
所示。
//將布局文件轉(zhuǎn)化View
final View view=View.inflate(this, R.layout.dio, null);
//設(shè)置builder顯示的View
builder.setView(view);
定義一個確定按鈕 圖5-2 自定義對話框布局文件
builder.setPositiveButton(“確定”, new DialogInterface. OnClickListenerO
一
//設(shè)置確定按鈕的點擊方法
public void onClick(DialogInterface dialog, int which){
//獲取自定義布局文件中的EditeText
EditText el=(EditText)view.findViewById(R.id.el);
//獲取EditText中輸入的字符串
String yhm=el.getText().toString();
//彈出一個提示框,顯示輸入的用戶名
Toast.makeText(getApplicationContext(),“輸入的用戶名是”+yhm,0).show();
}
}};
//創(chuàng)建并且顯示對話框
builder.create().show();
示例代碼:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/sky"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注冊"
android:textColor="@color/teal_200"
android:textSize="25dp"
android:gravity="center"
android:textStyle="bold"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/white"/>
<LinearLayout
android:id="@+id/w1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="QQ"
android:src="@drawable/qq"
android:textColor="@color/teal_200"
android:textSize="20sp" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@color/white"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="97dp"
android:layout_weight="1"
android:gravity="center"
android:text="微信"
android:src="@drawable/weixin"
android:textColor="@color/teal_200"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/white"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/youxiang">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用郵箱登錄"
android:textSize="30sp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓 名"
android:textColor="@color/teal_200"
android:textSize="20sp">
</TextView>
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入姓名">
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="郵 箱"
android:textColor="@color/teal_200"
android:textSize="20sp">
</TextView>
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入郵箱">
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼"
android:textColor="@color/teal_200"
android:textSize="20sp">
</TextView>
<EditText
android:id="@+id/password_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入密碼">
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓 別"
android:textColor="@color/teal_200"
android:textSize="20sp">
</TextView>
<RadioGroup
android:layout_marginLeft="30dp"
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textColor="@color/teal_200"
android:text="男"/>
<RadioButton
android:id="@+id/radio_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/teal_200"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="愛 好"
android:textColor="@color/teal_200"
android:textSize="20sp"/>
<CheckBox
android:layout_marginLeft="30dp"
android:id="@+id/cb_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戲"/>
<CheckBox
android:layout_marginLeft="30dp"
android:id="@+id/cb_basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="籃球"/>
<CheckBox
android:layout_marginLeft="30dp"
android:id="@+id/cb_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音樂"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="81dp"
android:gravity="center"
android:text="提交"
android:textColor="@color/teal_200"
android:textSize="25dp"
android:textStyle="bold"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/white"/>
</LinearLayout>
②MainActivity.java文章來源:http://www.zghlxwxcb.cn/news/detail-440373.html
package com.example.email;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MainActivity2 tel;
private EditText Name;
private EditText Phone;
private TextView Show;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tel = new MainActivity2(this);
Name = (EditText) findViewById(R.id.name);
Phone = (EditText) findViewById(R.id.phone);
Show = (TextView) findViewById(R.id.show);
mBtnAdd = (Button) findViewById(R.id.insert);
mBtnQuery = (Button) findViewById(R.id.query);
mBtnUpdate = (Button) findViewById(R.id.update);
mBtnDelete = (Button) findViewById(R.id.delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name;
String phone;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()) {
case R.id.insert: //添加數(shù)據(jù)
name = Name.getText().toString();
phone = Phone.getText().toString();
db = tel.getWritableDatabase();//獲取可讀寫SQLiteDatabse對象
values = new ContentValues(); // 創(chuàng)建ContentValues對象
values.put("name", name); // 將數(shù)據(jù)添加到ContentValues對象
values.put("phone", phone);
db.insert("information", null, values);
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.query: //查詢數(shù)據(jù)
db = tel.getReadableDatabase();
Cursor cursor = db.query("information", null, null, null, null,
null, null);
if (cursor.getCount() == 0) {
Show.setText("");
Toast.makeText(this, "沒有數(shù)據(jù)", Toast.LENGTH_SHORT).show();
} else {
cursor.moveToFirst();
Show.setText("Name : " + cursor.getString(1) + " ;Tel : " + cursor.getString(2));
}
while (cursor.moveToNext()) {
Show.append("\n" + "Name : " + cursor.getString(1) + " ;Tel : " + cursor.getString(2));
}
cursor.close();
db.close();
break;
case R.id.update: //更新數(shù)據(jù)
db = tel.getWritableDatabase();
values = new ContentValues(); // 要修改的數(shù)據(jù)
values.put("phone", phone = Phone.getText().toString());
db.update("information", values, "name=?",
new String[]{Name.getText().toString()}); // 更新并得到行數(shù)
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.delete: //刪除數(shù)據(jù)
db = tel.getWritableDatabase();
name = Name.getText().toString().trim();
phone = Phone.getText().toString().trim();
if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
Toast.makeText(this,"聯(lián)系人信息刪除失敗",Toast.LENGTH_SHORT).show();
}
else {
db.execSQL("delete from information where name=? and phone=?", new Object[]{name, phone});
Toast.makeText(this,"聯(lián)系人信息刪除成功",Toast.LENGTH_SHORT).show();
}
db.close();
break;
}
}
}
③MainActivity2.java文章來源地址http://www.zghlxwxcb.cn/news/detail-440373.html
package com.example.email;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MainActivity2 extends SQLiteOpenHelper {
public MainActivity2(Context context) {
super(context, "itcast.db", null, 1);
}
//當(dāng)數(shù)據(jù)庫第一次創(chuàng)建的時候執(zhí)行
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
到了這里,關(guān)于Android 平臺應(yīng)用軟件開發(fā)(學(xué)習(xí)中)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!