实例10:带选择头像的用户注册页面
来源:互联网 发布:js抛物线添加到购物车 编辑:程序博客网 时间:2024/05/16 03:17
1、布局文件
<LinearLayout 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:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/username" android:layout_width="180px" android:layout_height="50px" /> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" /> <EditText android:id="@+id/pwd" android:layout_width="180px" android:layout_height="50px" /> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认密码:" /> <EditText android:id="@+id/repwd" android:layout_width="180px" android:layout_height="50px" /> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="E-mail地址" /> <EditText android:id="@+id/email" android:layout_width="180px" android:layout_height="50px" /> </TableRow> </TableLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imgae" android:layout_width="200px" android:layout_height="200px"/> <Button android:id="@+id/select" android:text="选择头像" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout></LinearLayout>
2、在onCreate()方法中,获取选择投降按钮
Button button = (Button)findViewById(R.id.select);//获取"选择投降"按钮 button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this,HeadActivity.class);startActivityForResult(intent, 0x11);//启动指定的Activity}});
3、head,xml布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10px" android:horizontalSpacing="3px" android:verticalSpacing="3px" android:numColumns="4"></GridView></LinearLayout>
4、HeadActivity.java类
public class HeadActivity extends Activity {public int[] imageId = new int[]{R.drawable.head01,R.drawable.head02,R.drawable.head03,R.drawable.head04,R.drawable.head05,R.drawable.head06,R.drawable.head07,R.drawable.head08,R.drawable.head09};//定义并初始化保存头像id的数组@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.head);//设置该Activity使用的布局GridView gridView = (GridView)findViewById(R.id.gridView1);//获取GridView组件BaseAdapter adapter = new BaseAdapter() {@Overridepublic View getView(int position, View converView, ViewGroup parent) {ImageView imageView;//声明ImageView的对象if(converView==null){imageView = new ImageView(HeadActivity.this);//实例化ImageView的对象//设置图像的宽度和高度imageView.setAdjustViewBounds(true);imageView.setMaxWidth(158);imageView.setMaxHeight(150);imageView.setPadding(5, 5, 5, 5);//设置ImageView的内边距}else{imageView = (ImageView)converView;}imageView.setImageResource(imageId[position]);//为ImageView设置要显示的图片return imageView;//返回ImageView}/** * 获得当前选项的id */@Overridepublic long getItemId(int position) {return position;}/** * 获得当前选项 */@Overridepublic Object getItem(int position) {return position;}/** * 获得数量 * @return */@Overridepublic int getCount() {return imageId.length;}};gridView.setAdapter(adapter);//将适配器与GridView关联gridView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {Intent intent = getIntent();//获取Intent对象 Bundle bundle = new Bundle();//实例化传递的数据包bundle.putInt("iamgeId", imageId[position]);//显示选中的图片intent.putExtras(bundle);//将数据包保存在intent中setResult(0x11, intent);//设置返回的结果码,并返回调用该Acitivity的Activityfinish();//关闭当前的Activity}});}}
5、MainActivity中重写onActivityResult()方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0x11&&resultCode==0x11){//判断是否为待处理的结果 Bundle bundle = data.getExtras();//获取传递的数据包 int imageId = bundle.getInt("imageId");//获取选择的头像id //获取布局文件中添加的ImageView组件 ImageView iv = (ImageView)findViewById(R.id.imgae); iv.setImageResource(imageId);//显示选择的头像 } }
activity android:label="选择头像" android:icon="@drawable/ic_launcher" android:name=".HeadActivity"></activity>
0 0
- 实例10:带选择头像的用户注册页面
- 带选择头像的用户注册页面
- 带选择头像的用户注册界面
- Android学习进阶04——带选择头像的用户注册界面
- 注册用户选择头像及时显示啦。
- 用户注册时上传头像的实现
- JQUERY+MYSQL+PHP通用用户注册页面的实例
- JQUERY+MYSQL+PHP通用用户注册页面的实例
- JQUERY+MYSQL+PHP通用用户注册页面的实例
- 用户头像选择功能模块
- 用户注册页面的实现
- 带email认证的用户注册网站
- 一个简单的用户注册页面
- HTML简单的用户注册页面
- 带头像的用户注册页面
- node+vue实现用户注册--头像上传
- 设计用户注册页面
- 初步用户注册页面
- nginx 1.9 支持http2 协议
- MySQL5.6.12 Waiting for commit lock导致从库hang住的问题剖析
- CentOS7 上部署Elasticsearch,Fluentd and Kibana
- 提高篇 第9节-函数的嵌套使用
- 数据库表空间
- 实例10:带选择头像的用户注册页面
- 前端开发工具sumlime
- 1010. 一元多项式求导 (25)
- 学习写Android.mk
- Qt设计界面的初学笔记
- weblogic开发模式与生产模式
- android框架--xUtils
- Linux信号处理机制原理
- Gavua 之 cache