Android的UI布局

本代码是用Android线性布局实现的用户界面:

MainActivity:(代码部分)

<span style="font-size:18px;">package com.jerehedu.jerel0703;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private  EditText user,Pwd2;
    private  Button but;
    private RadioGroup rg;
    private CheckBox cb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //为Activity设置布局
        setContentView(R.layout.activity_main3);

        //根据编号查找。
        but = (Button)findViewById(R.id.but);

        Pwd2 = (EditText)findViewById(R.id.Pwd2);

        user = (EditText)findViewById(R.id.userName);

        rg = (RadioGroup)findViewById(R.id.rg);

        final ImageView iv =(ImageView)findViewById(R.id.iv);

        cb = (CheckBox) findViewById(R.id.ch1);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(getBaseContext(),
                            "您选择了"+cb.getText(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        //为rg添加监听事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //对所选ID进行判断。
                if(checkedId ==R.id.man){
//                    Toast.makeText(getBaseContext(),"男",Toast.LENGTH_SHORT).show();
                    iv.setImageResource(R.mipmap.allen);
                }else{
//                    Toast.makeText(getBaseContext(),"女",Toast.LENGTH_SHORT).show();
                    iv.setImageResource(R.mipmap.joker);
                }
            }
        });

        //为按钮设置一个点击监听事件
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,
                        user.getText(),
                        Toast.LENGTH_SHORT).show();//在MainActivity弹出,内容,弹出世间。
            }
        });
       //为条框添加监听器。
       Pwd2.setOnKeyListener(new View.OnKeyListener() {
           @Override
           public boolean onKey(View v, int keyCode, KeyEvent event) {
               //必须是松下Enter键。才可以执行以下代码
               if (KeyEvent.KEYCODE_ENTER == keyCode && event.getAction() == KeyEvent.ACTION_DOWN){
                   Toast.makeText(MainActivity.this,
                           user.getText(),
                           Toast.LENGTH_SHORT).show();//在MainActivity弹出,内容,弹出世间。
                   return true;
               }
               return false;
           }
       });
   }
}

</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">布局部分:</span>
<pre name="code" class="css"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户名"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/userName"
        android:hint="请输入用户名"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="密码" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/Pwd1"
        android:inputType="textPassword"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请再次输入密码"
        android:id="@+id/Pwd2"
        android:inputType="textPassword"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别"/>
    <!--线性布局(横向or竖向)的嵌套。-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="150sp"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rg"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:checked="true"
                android:id="@+id/man"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:id="@+id/woman"/>
            <!--对单选按钮进行编号ID实现单选-->
        </RadioGroup>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我的专业技能"/>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Java"
                android:id="@+id/ch1"/>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="IOS"
                android:id="@+id/ch2"/>

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Android"
                android:checked="true"
                android:id="@+id/ch3"/>
     </LinearLayout>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="123dp"
            android:src="@mipmap/allen"
            android:id="@+id/iv"/>
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/but"/>
</LinearLayout></span>
<span style="font-size:18px;">
</span>
运行界面:

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值