Android开发-控件

自己很久就想能在csdn上写点东西了,无奈没什么可以写的,不过到了大三才找到自己的方向也是够艰苦的了。以后的日子,博客里面应该就是安卓和web设计的东西了吧。让博文记录自己学习他们的过程。

内容:完成两个界面,包括图片的添加,用户名密码输入,button的使用,单选,下拉列表,日历。上图


自己设计了两个界面,通过register按键来切换界面,这里还没有实现对用户名和密码的验证,后续会做到的。

下面这个是登录界面的java代码,通过监听Register,来实现切换活动的效果。这里的findviewbyid是用来获取前台控件的一种方式,简单理解可以通过这种方式联系到

控件,实例化了控件,这样我们可以对它进行一些操作。

package com.example.chengww3.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.DatePicker;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends Activity {
    private EditText userName;
    private EditText userPass;
    private Button login;
    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        login = (Button)findViewById(R.id.login1);
        userName = (EditText)findViewById(R.id.user_name);
        userPass = (EditText)findViewById(R.id.user_pass);
        register =(Button)findViewById(R.id.register1);

        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public  void onClick(View v){
                //这里处理事件
                //新建一个Intend对象
                Intent intent = new Intent();
                //制定intent要启动的类
                intent.setClass(MainActivity.this, RegisterActivity.class);
                //启动一个新的activity
                startActivity(intent);
                //关闭当前的activity
                MainActivity.this.finish();
            }
        });
       
    }

}

下面这个是关于个人信息的界面的java代码,重点在于如何给下拉列表(spinner)赋值。

package com.example.chengww3.helloworld;

/**
 * Created by chengww3 on 2015/10/15.
 */
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class RegisterActivity extends Activity {
    private static final String[] dir = {"移动","软院","管理","政务","环境"};
    private Spinner m_spinner;
    private TextView m_textivew;
    private ArrayAdapter<String> adapter;
    public  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_main);

        m_textivew = (TextView) findViewById(R.id.TextView1);
        m_spinner = (Spinner) findViewById(R.id.Spinner1);

        //讲可选择的内容与ArrayAdapter连接
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,dir);
        //设置下拉列表的风格
        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_textivew.setText("你选择的方向是:" + dir[arg2]);
                //设置显示当前选择的项
                arg0.setVisibility(View.VISIBLE);
            }
            @Override
        public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
            }
        });
    }
}

静态的定义了一个string类型的数组。
 private static final String[] dir = {"移动","软院","管理","政务","环境"};

不过在跑安卓机的时候出现了下拉列表乱码的问题,下面也附上解决方案。因为自己使用的AS(Android studio)开发。所以不能保证对使用eclipse的朋友

有用,不过我还是推荐大家使用as,这是一种趋势吧。


在as左侧的项目栏里面有这些绿色的圈圈,找到第二个build.gradle。在里面最后一行添加:


就是规定编码方式,博主亲测GBK是可以的,utf-8还是会出现乱码。最后就附上两个xml文件吧,虽然感觉并没有什么卵用,大家都会的啦。

主界面xml:

<pre name="code" class="html"><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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="23sp"
            android:text="@string/welcome"
            android:textColor="#66ff00"/>

    </LinearLayout>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/crab">
    </ImageView>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="@string/username" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/user_name"
                android:hint="@string/hint"
                android:layout_weight="1" />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="@string/password" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:id="@+id/user_pass"
                android:layout_weight="1" />
        </TableRow>
    </TableLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/login1"
            android:text="@string/login" />
        <Button
            android:id="@+id/register1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/register"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></ProgressBar>
    </LinearLayout>


</LinearLayout>


 

下面是个人信息的xml设计:

<span style="font-size:10px;"><?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffff3cdb">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="18dp"
            android:text="@string/per_inf"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="200dp"
            android:text="@string/send"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginLeft="0dp"
            android:layout_gravity="left|center"
            android:src="@drawable/avatar">
        </ImageView>

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="30dp">


            <TableRow>
                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="@string/netid"
                    android:layout_weight="1" />
            </TableRow>

            <TableRow>
                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="@string/ture_name"
                    android:layout_weight="1" />
            </TableRow>
            <LinearLayout>
                <RadioGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/boy"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/girl"/>
                    </LinearLayout>

                </RadioGroup>
            </LinearLayout>
        </TableLayout>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chos_direc"/>
        <Spinner
            android:id="@+id/Spinner1"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chos_brith"/>
        <DatePicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </DatePicker>
    </RelativeLayout>

</LinearLayout>
</span>
当然里面还有一些string的定义,因为as会提示硬编码的warning,所以为了不烦恼,还是乖乖的去定义string吧。
<resources>
    <string name="app_name">FirstExperience</string>
    <string name="action_settings">settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="login">Login</string>
    <string name="register">Register</string>
    <string name="welcome">Welcome to First Android Class</string>
    <string name="username">UserName</string>
    <string name="password">Password</string>
    <string name="hint">Input your user account here</string>
    <string name="netid">学号</string>
    <string name="ture_name">姓名</string>
    <string name="per_inf">个人信息</string>
    <string name="send">提交</string>
    <string name="chos_brith">选择你的生日</string>
    <string name="chos_direc">选择你的方向</string>
    <string name="boy">男</string>
    <string name="girl">女</string>
    <string name="Register_true">Register_true</string>
</resources>


好啦,今天就写到这里啦,祝大家安卓学习愉快,附上项目的下载链接:
http://download.csdn.net/detail/qq_14908027/9210175


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值