1
Android
常用组件使用方法
2
Android
常用组件使用方法
................................
................................
................................
............
1
1. Intent
传参数
................................
................................
................................
................
3
2.
文本框
(textView)
................................
................................
................................
.........
3
3.
动态生成
UI
、读取联系人信息、
ListView
使用及
Toast
的位置设置
...................
3
4.
处理短信
................................
................................
................................
......................
3
5.
单项选择
................................
................................
................................
......................
5
6.
下拉列表
(Spinner)
的使用
................................
................................
...........................
6
7. sqlLite
的使用
................................
................................
................................
...............
9
8.
公用数据
................................
................................
................................
....................
10
9.
实做项目
................................
................................
................................
....................
10
3
1.
I
ntent
传参数
当
Activity
与
Activity/Service
参数传递,常用方法就是通过
Intent
实现
例子:
发送代码:
1.
Intent
intent
=
new
Intent(...);
2.
Bundle
bundle
=
new
Bundle();
3.
bundle.putString("param
",
"value");
4.
intent.putExtras(bundle);
5.
context.startActivity(intent);
或
context.startService(intent
);
接收代码:
1.
Bundle
bunde
=
intent.getExtras();
2.
String
name
=
bunde.getInt("param");
注意键值要一致。
2.
文本框
⡴數瑖楥w)
3.
动态生成
啉
、读取联系人信息、
䱩L瑖楥i
使用及
Toa獴
的
位置设置
4.
处理短信
<?xml version="1.0" encoding="utf
-
8"?>
4
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yarin.android.Examples_04_05"
android:versionCode="1"
android:versionName="1.0">
<uses
-
permission and
roid:name="android.permission.RECEIVE_SMS"></uses
-
permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity01"
android:label="@string/app_name">
<int
ent
-
filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent
-
filter>
</activity>
<receiver android:name=".SMSReceiver" android:enab
led="true">
<intent
-
filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent
-
filter>
</receiver>
</application>
<uses
-
sdk android:minSdkVersion="5" />
</manifest>
代码:
public class SMSReceiver extends Broad
castReceiver
{
/*
当收到短信时,就会触发此方法
*/
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int
n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
//
产生一个
Toast
Toast toast = Toast.makeText(context, "
短 信 内 容
: " +
smsMessage[0].getMessageBody(), Toast.LENGTH_LONG)
.show();
}
}
5
5.
单项选择
<?xml ver
sion="1.0" encoding="utf
-
8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/Te
xtView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_con
tent"
android:orientation="vertical"
android:layout_x="3px"
android:layout_y="54px"
>
<RadioButton
android:id="@+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:
text="@string/RadioButton1"
/>
<RadioButton
android:id="@+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RadioButton2"
/>
<RadioButton
android:id
="@+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RadioButton3"
/>
<RadioButton
android:id="@+id/RadioButton4"
android:layout_width="wrap_content"
6
android:layout_height="wrap_content"
android:text="@string/RadioButton4"
/>
</RadioGroup>
</LinearLayout>
代码:
m_TextView = (TextView) findViewById(R.id.TextView01);
m_RadioGroup = (RadioGroup) findViewById(R.id.RadioGroup01);
m_Radio1 = (RadioButton) findViewById(R.id.RadioButton1);
m_Radio2 = (RadioButton) findViewById(R.id.RadioButton2);
m_Radio3 =
(RadioButton) findViewById(R.id.RadioButton3);
m_Radio4 = (RadioButton) findViewById(R.id.RadioButton4);
/*
设置事件监听
*/
m_RadioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(R
adioGroup group, int checkedId)
{
// TODO Auto
-
generated method stub
if (
checkedId == m_Radio2.getId()
)
{
DisplayToast("
正确答案:
" + m_Radio2.getText() + "
,恭喜你,回答正
确!
");
}
else
{
DisplayToast("
请注意,回答错误!
");
}
}
});
6.
下拉列表
⡓灩湮敲(
的使用
<?xml version="1.0" encoding="utf
-
8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Te
xtView
7
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/Spinner1"
android:layout_width="wrap_content"
android:layout_hei
ght="wrap_content"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
代码
m_TextView = (TextView) findViewById(R.id.TextView1);
m_Spinner = (Spinner) findViewById(R.id.Spinner1);
//
将可选内容与
ArrayAdapter
连接
adapter = new ArrayAdapter<String>(thi
s, android.R.layout.simple_spinner_item,
m_Countries);
//
设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//
将
adapter
添加到
m_Spinner
中
m_Spinner.setAdapter(adapter);
//
添加
Spinner
事件监听
m_Spinner.setOnItemSe
lectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
m_TextView.setText("
你的血型是:
" + m_Countries[arg2]);
//
设置显示当前选择的项
arg0.setVisibility(View.V
ISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto
-
generated method stub
}
8
});
7. Shared Preference
数据存储
保存:
//
取得活动的
preferences
对象
.
SharedPreferences uiState = getPreferences(0);
//
取得编辑对象
SharedPreferences.Editor editor = uiState.edit();
//
添加值
editor.putBoolean("bmusic", mbMusic);
//
提交保存
editor.commit();
读取:
SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
Boolean
mbMusic = settings.getBoolean("bmu
sic", false);
存放文件位置:
/data/data shared_prefs
文件内
8.
文件存储
装载、读取数据:
void load()
{
/*
构建
Properties
对对象
*/
Properties properties = new Properties();
try
{
/*
开发文件
*/
FileInputStream stream = this.openFileInput("music.cfg");
/*
读取文件
内容
*/
properties.load(stream);
}
catch (FileNotFoundException e)
9
{
return;
}
catch (IOException e)
{
return;
}
/*
取得数据
*/
mbMusic = Boolean.valueOf(properties.get("bmusic").toString());
}
保存数据:
boolean save()
{
Properties
properties = new Properties();
/*
将数据打包成
Properties */
properties.put("bmusic", String.valueOf(mbMusic));
try
{
FileOutputStream stream = this.openFileOutput("music.cfg",
Context.MODE_WORLD_WRITEABLE);
/*
将打包好的数据写入文件中
*/
properties.
store(stream, "");
}
catch (FileNotFoundException e)
{
return false;
}
catch (IOException e)
{
return false;
}
return true;
}
9
. sqlLite
的使用
10
10
.
公用数据
11
.
实做项目
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Σχόλια 0
Συνδεθείτε για να κοινοποιήσετε σχόλιο