当前位置:首页 » 《随便一记》 » 正文

实验 4 Android常用控件的应用(二)_Runnable_001的博客

1 人参与  2022年02月21日 13:59  分类 : 《随便一记》  评论

点击全文阅读


废话不多说,直接上代码供参考:

  • shiyan0401
    1. activity_main.xml
    2. ActivityMain.java

  • shiyan0402
    1. activity_main.xml
    2. ActivityMain.java

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="center_horizontal"
        android:text="学生信息登记"
        android:textColor="#f00"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:text="姓名:"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:hint="请输入姓名" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:text="年龄:"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_age"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:hint="请输入年龄" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_major"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="专业:"
            android:textSize="20sp" />

        <RadioGroup
            android:id="@+id/rg_major"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <RadioButton
                android:id="@+id/rb_jk"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="计算机科学与技术" />

            <RadioButton
                android:id="@+id/rb_rj"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="软件工程" />

            <RadioButton
                android:id="@+id/rb_wl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="网络工程" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/hobbys"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好:"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_sing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="唱歌" />

        <CheckBox
            android:id="@+id/cb_dance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳舞" />

        <CheckBox
            android:id="@+id/cb_travel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旅游" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="60dp"
            android:layout_weight="1"
            android:background="#0A0"
            android:text="录入"
            android:textSize="20sp"
            android:onClick="doClick" />
         <Button
            android:id="@+id/bt_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00f0f0"
            android:text="清空"
            android:textSize="20sp" 
            android:onClick="doClick"/>

       
    </LinearLayout>

    <TextView
        android:id="@+id/tv_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="信息显示:"
        android:textSize="20sp" />
    <Space />

    <TextView
        android:id="@+id/tv_showinfo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_columnSpan="2"
        android:singleLine="false"
        android:textColor="#6495ED" />

</GridLayout>
ActivityMain.java:
package com.example.shiyan0401;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText name, age;
	private RadioGroup rg_major;
	private RadioButton[] major = new RadioButton[3];
	private LinearLayout hobby;
	private CheckBox[] cb_hobby = new CheckBox[3];
	private TextView tv_showInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		name = (EditText) findViewById(R.id.et_name);
		age = (EditText) findViewById(R.id.et_age);
		rg_major = (RadioGroup) findViewById(R.id.rg_major);
		major[0] = (RadioButton) findViewById(R.id.rb_jk);
		major[1] = (RadioButton) findViewById(R.id.rb_rj);
		major[2] = (RadioButton) findViewById(R.id.rb_wl);
		hobby = (LinearLayout) findViewById(R.id.hobbys);
		cb_hobby[0] = (CheckBox) findViewById(R.id.cb_sing);
		cb_hobby[1] = (CheckBox) findViewById(R.id.cb_dance);
		cb_hobby[2] = (CheckBox) findViewById(R.id.cb_travel);
		tv_showInfo = (TextView) findViewById(R.id.tv_showinfo);
	}

	public void doClick(View view) {
		Integer id = view.getId();
		if (id.equals(R.id.bt_reg)) {
			String strName = name.getText().toString().trim();
			String strAge = age.getText().toString().trim();
			String strMajor = "";
			String strHobbies = "";
			for (int i = 0; i < rg_major.getChildCount(); i++) {
				RadioButton r = (RadioButton) rg_major.getChildAt(i);
				if (r.isChecked()) {
					strMajor = r.getText().toString().trim();
					break;
				}
			}
			for (int i = 1; i < hobby.getChildCount(); i++) {
				CheckBox r = (CheckBox) hobby.getChildAt(i);
				if (r.isChecked()) {
					strHobbies += r.getText().toString().trim() + " ";
				}
			}
			reg(strName, strAge, strMajor, strHobbies);
		} else {
			reset();
		}
	}

	// 登记
	private void reg(String... args) {
		// 判断信息是否为空
		for (String s : args) {
			if (s.isEmpty()) {
				Toast.makeText(this, "必填信息不能为空!", Toast.LENGTH_LONG).show();
				return;
			}
		}

		String message = "姓名: " + args[0] + "\n年龄: " + args[1] + "\n专业: " + args[2] + "\n爱好: " + args[3];
		tv_showInfo.setText(message);
	}

	// 重置
	private void reset() {
		name.setText("");
		age.setText("");
		for (RadioButton r : major) {
			r.setChecked(false);
		}

		for (CheckBox cb : cb_hobby) {
			cb.setChecked(false);
		}

		tv_showInfo.setText("");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

附图:
在这里插入图片描述

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" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="图片处理"
        android:textColor="#DDA0DD"
        android:textSize="30sp" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="400px"
        android:layout_height="400px"
        android:layout_below="@id/tv1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:alpha="25"
        />

    <Button
        android:id="@+id/bt_sub"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_marginTop="20dp"
        android:background="#00BFFF"
        android:text="透明度降低"
        android:textSize="15sp" 
        android:onClick="doClick"
        />

    <Button
        android:id="@+id/bt_pre"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/bt_sub"
        android:background="#F08080"
        android:text="上一张"
        android:textSize="15sp" 
        android:onClick="doClick"
        />

    <Button
        android:id="@+id/bt_next"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/bt_pre"
        android:background="#F08080"
        android:text="下一张"
        android:textSize="15sp"
        android:onClick="doClick"
         />
    <Button
        android:id="@+id/bt_add"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/bt_next"
        android:background="#00BFFF"
        android:text="透明度增加"
        android:textSize="15sp"
        android:onClick="doClick"
         />

</RelativeLayout>
ActivityMain.java:
package com.example.shiyan0402;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ImageView iv ;
	private final Integer[] images = {R.drawable.p1,R.drawable.p2,R.drawable.p3};
	private int index = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView)findViewById(R.id.iv);
		iv.setImageResource(images[index]); //启动默认显示数组第一张图片
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void doClick(View view){
		Integer id = view.getId();
		switch (id) {
		case R.id.bt_sub:
			diaphaneitySub();
			break;
		case R.id.bt_pre:
			preImage();
			break;
		case R.id.bt_next:
			nextImage();
			break;
		case R.id.bt_add:
			diaphaneityAdd();
			break;
		}
	}

	private void diaphaneitySub(){
		float alpha = iv.getAlpha();
		if(alpha >= 5){
			alpha -= 5;
		}else{
			Toast.makeText(this, "已是最低透明度!alpha:"+alpha, Toast.LENGTH_LONG).show();
			return;
		}
		iv.setAlpha(alpha);
	}
	private void diaphaneityAdd(){
		float alpha = iv.getAlpha();
		if(alpha < 255){
			alpha += 5;
		}else{
			Toast.makeText(this, "已是最高透明度!alpha:"+alpha, Toast.LENGTH_LONG).show();
			return;
		}
		iv.setAlpha(alpha);
	}
	
	private void preImage(){
		/*
		if(index == 0){
			Toast.makeText(this, "已是第一张图片了!", Toast.LENGTH_LONG).show();
		}else{
			iv.setImageResource(images[--index]);
		}
		*/
		if(index == 0){
			index = images.length-1;
			iv.setImageResource(images[index]);
		}else{
			iv.setImageResource(images[--index]);
		}
		
	}
	
	private void nextImage(){
		/*
		if(index < images.length-1){
			iv.setImageResource(images[++index]);
		}else{
			Toast.makeText(this, "已是最后一张图片了!", Toast.LENGTH_LONG).show();
		}
		*/
		if(index == images.length-1){
			index = 0;
			iv.setImageResource(images[index]); 
		}else{
			iv.setImageResource(images[++index]);
		}
	}
}

附图:
在这里插入图片描述


点击全文阅读


本文链接:http://zhangshiyu.com/post/35099.html

已是  透明度  信息  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1