上午主要是帮忙解决其他项目的测试bug,只能下午学习安卓咯!
今天完成的是下拉列表的实现,代码基本上是网上和书上看来的,没有偷懒直接copy,自己一个个敲出来的。
代码写完后,给老徐看,他气的想骂我,说到处都是错的,最后帮我调试OK,其实书本上的也不是完全正确呀。
下面是代码及笔记。
my_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="300"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="300">
</alpha>
</set>
myspinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myspinner"
android:layout_width="match_parent"
android:layout_height="24sp"
android:singleLine="true"
style="?android:attr/spinnerDropDownItemStyle" />
rounded_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp" >
<solid android:color="#FFFFFF" />
<corners
android:topRightRadius="5dp"
android:topLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:width="2dp" />
<stroke
android:width="2dp"
android:color="#dcdcdc" />
</shape>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- 请输入昵称 -->
<EditText
android:id="@+id/EntryUsername"
android:layout_width="260dp"
android:layout_height="40dp"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:hint="@string/UserName"
android:inputType="textShortMessage"
android:background="@drawable/rounded_edit"/>
<requestFocus />
<!-- 请输入手机号码 -->
<EditText
android:id="@+id/EntryPhoneNum"
android:layout_width="260dp"
android:layout_height="40dp"
android:layout_marginTop="110dp"
android:layout_centerHorizontal="true"
android:hint="@string/MobilePhone"
android:inputType="phone"
android:background="@drawable/rounded_edit"/>
<requestFocus />
<!-- 选择部门-->
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:id="@+id/SelectedItem"
android:text="@string/title"
android:layout_marginTop="170dp"
android:textColor="#1C9ACD"
android:textSize="25sp"/>
<Spinner
android:id="@+id/SelectDepartment"
style="?android:attr/spinnerStyle"
android:layout_width="260dp"
android:layout_height="40dp"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:background="@drawable/rounded_edit"/><!-- 水平居中 -->
</RelativeLayout> <!-- 相对布局 -->
simple_spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/** //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee" />
simple_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/** //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
MainActivity.java
package com.pxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this,SpinnerActivity.class));
}
}
SpinnerActivity.java
package com.pxx;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class SpinnerActivity extends Activity {
private List<String> list = new ArrayList<String>();
private Spinner spinner = null;
private TextView textView = null;
private ArrayAdapter<String> adapter = null;
View v1;
Animation myAnimation;
boolean flag = false; //默认为false
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState); //调用父类的构造函数
/* 载入activity_main.xml Layout */
v1 = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(v1);
list.add("财险");
list.add("寿险");
list.add("IT");
list.add("市场");
spinner = (Spinner)findViewById(R.id.SelectDepartment);
textView = (TextView)findViewById(R.id.SelectedItem);
adapter = new ArrayAdapter<String>(this,R.layout.simple_spinner_item,list);
/* simple_spinner_dropdown_item为下拉菜单样式 */
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
/* 将ArrayAdapter 添加到Spinner对象中*/
spinner.setAdapter(adapter);
/*为spinner添加选择事件监听器setOnItemSelectedListener*/
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(flag == true){
textView.setText("您选择的部门是"+adapter.getItem(arg2).toString());
/* 显示spinner*/
arg0.setVisibility(View.VISIBLE);
}
flag =true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0){
textView.setText("未选择");
arg0.setVisibility(View.INVISIBLE);
}
});
/*取得Animation定义在res/main目录下*/
myAnimation = AnimationUtils.loadAnimation(this,R.anim.my_anim);
/*为下拉菜单的内容进行触屏事件处理*/
spinner.setOnTouchListener(new Spinner.OnTouchListener(){
@Override
public boolean onTouch(View v ,MotionEvent event){
/*将spinner运行Animation*/
v.startAnimation(myAnimation);
/*隐藏spinner*/
v.setVisibility(View.VISIBLE);
return false;
}
});
/* 下拉菜单弹出的内容选项焦点改变事件处理 */
spinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
public void OnFocusChange(View v,boolean hasFocus){
v.setVisibility(View.VISIBLE);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
});
}
}