Android实验二简单事件处理

activity_mian.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="zhku.edu.exp2_2.MainActivity"
    android:id="@+id/relativelayout1">

    <RelativeLayout
        android:id="@+id/relativelayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:id="@+id/title"
        android:text="@string/signin_pro"
        android:textSize="20sp"
        android:gravity="center_vertical"
        android:background="@drawable/top_bg"/>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_below="@+id/relativelayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >

        <ImageView
            android:id="@+id/profile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/profile"
            />

        <RadioGroup
            android:id="@+id/radiogroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_toRightOf="@id/profile">

            <RadioButton
                android:id="@+id/student"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/student"
                android:checked="true"
                />
            <RadioButton
                android:id="@+id/worker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/worker"/>

        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/linearlayout1">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:paddingLeft="10dp"
            android:textSize="15sp"
            android:singleLine="true"
            android:id="@+id/username"
            android:hint="姓名"
            android:background="@drawable/single_edittext"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingLeft="10dp"
            android:textSize="15sp"
            android:singleLine="true"
            android:id="@+id/phone"
            android:hint="手机号"
            android:inputType="phone"
            android:background="@drawable/top_edittext"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:paddingLeft="10dp"
            android:textSize="15sp"
            android:singleLine="true"
            android:id="@+id/password"
            android:hint="密码"
            android:inputType="textPassword"
            android:background="@drawable/bottom_edittext"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/linearlayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hobby"
            android:text="@string/hobby"/>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/program"
            android:text="@string/program"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/swim"
            android:text="@string/swim"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/run"
            android:text="@string/run"/>


    </LinearLayout>
    <Button
        android:id="@+id/register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearlayout3"
        android:text="@string/register"
        android:background="@drawable/dialog_up"
        android:layout_centerHorizontal="true"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/msg_show"/>
</RelativeLayout>

string.xml:
<resources>
    <string name="app_name">exp2_2</string>
    <string name="signin_pro">注册个人资料</string>
    <string name="student">学生</string>
    <string name="worker">工人</string>
    <string name="hobby">兴趣:</string>
    <string name="program">编程</string>
    <string name="swim">游泳</string>
    <string name="run">跑步</string>
    <string name="register">注册</string>
</resources>


MainActivity.java
package zhku.edu.exp2_2;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
    private RadioGroup rg;
    private Button btregister;
    private RadioButton rbstudent;
    private  RadioButton rbworker;
    private EditText etname;
    private EditText etphone;
    private EditText etpassword;
    private CheckBox cbprogram ;
    private CheckBox cbswim ;
    private CheckBox cbrun ;
    private TextView tvshow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etname=(EditText) findViewById(R.id.username);
        etphone=(EditText)findViewById(R.id.phone);
        etpassword=(EditText)findViewById(R.id.password);
        tvshow=(TextView) findViewById(R.id.msg_show);

        cbprogram=(CheckBox)findViewById(R.id.program) ;
        cbswim=(CheckBox)findViewById(R.id.swim) ;
        cbrun=(CheckBox)findViewById(R.id.run) ;

        btregister=(Button)findViewById(R.id.register);
        rg=(RadioGroup)findViewById(R.id.radiogroup1) ;
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb=(RadioButton)findViewById(checkedId);
                String str=rb.getText().toString();
                Toast.makeText(MainActivity.this,"你是"+str,Toast.LENGTH_SHORT).show();
            }
        });
        btregister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String sName=etname.getText().toString().trim();
                if(TextUtils.isEmpty(sName))
                    tvshow.setText("用户名不能为空");
                else
                    tvshow.setText("你的用户名为:"+sName);
                if(etphone.length()==11||etphone.length()==8)
                    tvshow.append(",手机号为:"+etphone.getText().toString());
                else
                    tvshow.append("你的手机号不正确");

                tvshow.append("你的爱好为:");
                if(cbprogram.isChecked()) {
                    tvshow.append("编程");
                }
                if(cbswim.isChecked()){
                    tvshow.append(",游泳");
                }
                if(cbrun.isChecked()) {
                    tvshow.append(",跑步");
                }
            }
           });
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值