Android应用开发揭秘(笔记) 第四章用户界面开发(第一部分)

来源:互联网 发布:三星s7开发者选项优化 编辑:程序博客网 时间:2024/05/21 17:36
☆用户界面开发详解
1.Android应用程序界面通常使用View和ViewGroup控件配XML样式来进行设计。
2.Android事件包括按钮事件、触屏事件以及一些高级控件的事件监听。
3.Android生成屏幕的三种方式:xml配置生成;通过用户界面接口生成;直接用代码生成。android应用中,用户界面是由View和ViewGroup对象构建的。
△ View:任何一个view对象都是继承android.view.View。View是为Widget服务的。Widget包括View,EditText,Button,RadioButton,CheckBox和ScrollView等。
△ ViewGroup:是一个android.view.ViewGroup的对象。在ViewGroup中子对象可能会请求获得它们的在父对象的大小和位置,但是父对象对每个子对象的大小和位置
有最终决定权。
4.时间处理:事件就是用户与UI交互时所触发的操作。在Android下,View同样可以相应按键和触屏事件。
boolean onKeyDown( int keyCode, KeyEvent event );//用于响应按键按下事件。
onKeyDown用法如下:
public boolean onKeyDown(int keyCode, KeyEvent event){
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
do something...
break;

case ...
break;
}
return super.onKeyDown(keyCode, event);
}

boolean onKeyMultiple( int keyCode, int repeatCount, KeyEvent event );//用于响应按键重复点击事件。
boolean onKeyUp( int keyCod, KeyEvent event );//用于响应按键伸开。
boolean onTouchEvent( MotionEvent event );//用于响应触屏事件。
onTouchEvent用发如下:
public boolean onTouchEvent(MotionEvent event)
{
int iAction = event.getAction();
if (iAction == MotionEvent.ACTION_CANCEL ||
iAction == MotionEvent.ACTION_DOWN ||
iAction == MotionEvent.ACTION_MOVE)
{
return false;
}
//得到触笔点击的位置
int x = (int) event.getX();
int y = (int) event.getY();

DisplayToast("触笔点击坐标:("+Integer.toString(x)+","+Integer.toString(y)+")");

return super.onTouchEvent(event);
}

在Android中控件事件是通过某些控件的监听器监听并重写某些函数来处理的。
这里我们使用构造方法来构造一个KeyEvent对象,让起键值为“KeyEvent.KEYCODE.BACK”,这样不管我们在按任意键时都执行退出操作。
代码如下:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//这里构建KeyEvent对象,其功能为返回键的功能
//因此我们按任意键都会执行返回键功能
KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);

//这里传入的参数就是我们自己构建的KeyEvent对象key
return super.onKeyDown(key.getKeyCode(), key);
}

☆常用控件应用
1.TextView:用来显示一个文本标签的控件。
代码中获取TextView对象,代码如下:
textview = (TextView)this.findViewById(R.id.textview);
/* 设置文本的颜色 */
textview.setTextColor(Color.RED);
/* 设置字体大小 */
textview.setTextSize(20);
/* 设置文字背景 */
textview.setBackgroundColor(Color.BLUE);
/* 设置TextView显示的文字 */
textview.setText(string);
在xml中布局代码:
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
2.ListView:用来显示一个列表的控件。当鼠标滚动时会触发setOnItemSelectedListener事件,点击时则会触发setOnItemClickListener事件。
ListView代码如下:
LinearLayout m_LinearLayout;
ListView m_ListView;
public void onCreate(Bundle savedInstanceState)
{
/* 创建LinearLayout布局对象 */
m_LinearLayout = new LinearLayout(this);
/* 设置布局LinearLayout的属性 */
m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);

/* 创建ListView对象 */
m_ListView = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
m_ListView.setBackgroundColor(Color.BLACK);

/* 添加m_ListView到m_LinearLayout布局 */
m_LinearLayout.addView(m_ListView, param);

/* 设置显示m_LinearLayout布局 */
setContentView(m_LinearLayout);

// 获取数据库Phones的Cursor
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);

// ListAdapter是ListView和后台数据的桥梁
ListAdapter adapter = new SimpleCursorAdapter(this,
// 定义List中每一行的显示模板
// 表示每一行包含两个数据项
android.R.layout.simple_list_item_2,
// 数据库的Cursor对象
cur,
// 从数据库的NAME和NUMBER两列中取数据
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER },
// 与NAME和NUMBER对应的Views
new int[] { android.R.id.text1, android.R.id.text2 });

/* 将adapter添加到m_ListView中 */
m_ListView.setAdapter(adapter);

/* 为m_ListView视图添加setOnItemSelectedListener监听 */
m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{

DisplayToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项");
}

@Override
public void onNothingSelected(AdapterView<?> arg0)
{
//没有选中
}
});

/* 为m_ListView视图添加setOnItemClickListener监听 */
m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
//于对选中的项进行处理
DisplayToast("选中了第"+Integer.toString(arg2+1)+"项");
}

});

}
}

3.Toast:是Android提供的“快显讯息”类。
用法如下:
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

4.EditText:实现编辑框,供用户输入信息。
代码中获取EditText对象,代码如下:
m_EditText = (EditText) findViewById(R.id.EditText01);
/**
* 设置当m_EditText中为空时提示的内容
* 在XML中同样可以实现:android:hint="请输入账号"
*/
m_EditText.setHint("请输入账号");

/* 设置EditText事件监听 */
m_EditText.setOnKeyListener(new EditText.OnKeyListener() {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2)
{
// TODO Auto-generated method stub
// 得到文字,将其显示到TextView中
//m_TextView.setText("文本框中内容是:" + m_EditText.getText().toString());
return false;
}
});
在xml中布局代码:
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="29px"
android:layout_y="33px"
/>

5.单项选择(RadioGroup、RadioButton):单项选择的组件。
代码中获取RadioGroup,RadioButton对象及用法,代码如下:

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(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if (checkedId == m_Radio2.getId())
{
DisplayToast("正确答案:" + m_Radio2.getText() + ",恭喜你,回答正确!");
}
else
{
DisplayToast("请注意,回答错误!");
}
}
});
在xml中布局代码:
<RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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"
android:layout_height="wrap_content"
android:text="@string/RadioButton4"
/>
</RadioGroup>

6.多项选择(CheckBox):用来实现多项选择的组件。
用法:先在布局文件中第一CheckBox来实现多项选择,然后对每一项事件监听setOnCheckedChangeListener,通过isChecked来判断选项是否被选中。
代码如下:
CheckBox m_check_box1;
CheckBox m_check_box2;
CheckBox m_check_box3;
CheckBox m_check_box4;

m_check_box1 = (CheckBox)findViewById(R.id.checkbox1);
........
m_check_box4 = (CheckBox)findViewById(R.id.checkbox1);

m_check_box1.setOnCheckedChangeListener( new CheckBox.OnCheckedChangleListner(){
public void onCheckChanged( CompoundButton buttonView, bool isChecked ){
if( m_check_box1.isChecked() ){
do something....
}

}
});
通过isChecked()来判断当前选项是否被选中。
在xml中布局代码:
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/checkbox1"
>
</CheckBox>

m_check_box2,m_check_box3,m_check_box4 setOnCheckedChangeListener 用法同上。

7.下拉列表(Spinner):
编码实现:首先需要在布局中定义Spinner组件,然后将可选内容通过ArrayAdapter和下拉列表连接起来,通过监听setOnItemSelectedListener并实现
onItemSelected,获取用户选择的内容,最后通过setVisibility方法设置当前显示项。
代码实现:
private static final String[] m_Countries = { "O型", "A型", "B型", "AB型", "其他" };

private Spinner m_Spinner;

m_Spinner = (Spinner) findViewById(R.id.Spinner1);
//将可选内容与ArrayAdapter连接
adapter = new ArrayAdapter<String>(this, 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.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
m_TextView.setText("你的血型是:" + m_Countries[arg2]);
//设置显示当前选择的项
arg0.setVisibility(View.VISIBLE);
}

@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}

});

在xml中布局代码:
<Spinner
android:id="@+id/Spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>

8.自动提示 (省略)

9.日期和时间(DatePicker,TimePicker):DatePicker用来设置日期,TimePicker用来设置时间。
编码实现:首先需要在布局文件中定义DatePicker和TimePicker,然后通过Calendar类获取系统时间,接着通过init的方法将日期传递给DatePicker,
并设置onDateChangedListener来监听日期改变,当时间被修改时需要设置setOnTimeChangedListener监听来设置时间。
代码如下:
//声明DatePicker对象
DatePicker m_DatePicker;
//声明TimePicker对象
TimePicker m_TimePicker;

c=Calendar.getInstance();

//获取DatePicker对象
m_DatePicker = (DatePicker) findViewById(R.id.DatePicker01);
//将日历初始化为当前系统时间,并设置其事件监听
m_DatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
//当日期更改时,在这里处理
//c.set(year, monthOfYear, dayOfMonth);
}
});

//获取TimePicker对象
m_TimePicker = (TimePicker) findViewById(R.id.TimePicker01);
//设置为24小时制显示
m_TimePicker.setIs24HourView(true);

//监听时间改变
m_TimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
//时间改变时处理
//c.set(year, month, day, hourOfDay, minute, second);
}
});

在xml中布局代码:
<DatePicker android:id="@+id/DatePicker01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></DatePicker>

<TimePicker android:id="@+id/TimePicker01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TimePicker>

10.按钮(Button):需要对按钮设置setOnClickListener事件监听。
代码如下:
Button m_Button1=(Button)findViewById(R.id.Button1);
m_Button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//do something
}
});

在xml中布局代码:
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮"
>
</Button>

原创粉丝点击