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

1.shiyan0401

string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="action_settings">Settings</string>
    
    <string name="titile">学生信息登记</string>
    <string name="stu_name">姓名:</string>
    <string name="stu_age">年龄:</string>
    <string name="stu_result">信息显示:</string>
    
    <string name="hint_name">请输入姓名</string>
    <string name="hint_age">请输入年龄</string>
    
    <string name="stu_class">专业:</string>
    <string name="radio_jisaunji">计算机科学与技术</string>
    <string name="radio_ruanjian">软件工程</string>
    <string name="radio_wangluo">网络工程</string>
    
    <string name="stu_hobby">爱好:</string>
    <string name="checkbox_sing">唱歌</string>
    <string name="checkbox_dance">跳舞</string>
    <string name="checkbox_swim">游泳</string>
    
    
    
    <string name="btn_enregister">登记</string>
    <string name="btn_resetting">重置</string>

</resources>

activity_main.xml
<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: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"
    android:orientation="vertical"
    android:gravity="center_horizontal" 
    android:padding="10dp">
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:textColor="#f00"
        android:text="@string/titile"/>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

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

        <EditText
            android:id="@+id/edit_name"
            android:inputType="text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:hint="@string/hint_name"
            android:singleLine="true" >
        </EditText>

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

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

        <EditText
            android:id="@+id/edit_age"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:inputType="date"
            android:hint="@string/hint_age"
            android:singleLine="true" >
        </EditText>

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

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

        <RadioGroup 
	        android:id="@+id/classRadioGroup"
	        android:orientation="vertical"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content">
        
	        <RadioButton 
	            android:id="@+id/jisuanjiRadio"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="@string/radio_jisaunji"/>
	        
	        <RadioButton 
	            android:id="@+id/ruanjianjiRadio"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="@string/radio_ruanjian"/>
	        
	        <RadioButton 
	            android:id="@+id/wangluoRadio"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="@string/radio_wangluo"/>
   		 </RadioGroup>

    </LinearLayout>
    
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/stu_hobby"
            android:layout_gravity="center_vertical"/>
        
        <LinearLayout 
            android:id="@+id/hobbycheckboxs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            
            <CheckBox 
            android:id="@+id/singCheckbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/checkbox_sing"/>
        
	        <CheckBox 
	            android:id="@+id/danceCheckbox"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="@string/checkbox_dance"/>
	        
	        <CheckBox 
	            android:id="@+id/swimCheckbox"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="@string/checkbox_dance"/>
        </LinearLayout>
        
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right|bottom"
        >
        
        <Button 
            android:id="@+id/btn_enregister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_enregister"
            style="?android:attr/buttonStyle"
            android:onClick="click_enregister"/>
        <Button 
            android:id="@+id/btn_restting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_resetting"
            style="?android:attr/buttonStyle"
            android:onClick="click_restting"/>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/stu_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#444693"
            android:textSize="20sp"
            android:text="@string/stu_result" />
        
        <TextView
            android:id="@+id/txt_result"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:singleLine="false" />

    </LinearLayout>
    

</LinearLayout>

MainActivity.java
package com.example.shiyan0401;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
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 {
	
	EditText stu_name;
	EditText stu_age;
	RadioGroup stu_class;
	TextView result;
	Button enregister;
	Button restting;
	LinearLayout hobbycheckbox;
	CheckBox bobbyCb[] = new CheckBox[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        stu_name = (EditText)findViewById(R.id.edit_name);
        stu_age = (EditText)findViewById(R.id.edit_age);
        result = (TextView)findViewById(R.id.txt_result);
        stu_class = (RadioGroup)findViewById(R.id.classRadioGroup);
        hobbycheckbox = (LinearLayout)findViewById(R.id.hobbycheckboxs);
        
        for(int i = 0; i < hobbycheckbox.getChildCount(); i++) {
        	bobbyCb[i] = (CheckBox)hobbycheckbox.getChildAt(i);
        }
        
        enregister = (Button)findViewById(R.id.btn_enregister);
        restting = (Button)findViewById(R.id.btn_restting);
        
        enregister.setOnClickListener(new regstListened());
        restting.setOnClickListener(new resttingListened());
    }


    @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;
    }
    
    class regstListened implements OnClickListener {

		@Override
		public void onClick(View v) {
			String radio_result = "";
			String checkbox_result = "";
			for(int i = 0; i < stu_class.getChildCount(); i++) {
				RadioButton r = (RadioButton)stu_class.getChildAt(i);
				if(r.isChecked()){
					radio_result = r.getText().toString();
					break;
				}
			}
			for(int i = 0; i < bobbyCb.length; i++) {
				if(bobbyCb[i].isChecked()) {
					checkbox_result += bobbyCb[i].getText().toString();
				}
			}
			
			result.setText("姓名:" + stu_name.getText().toString() +"\n年龄:" + stu_age.getText().toString()
					+ "\n专业:" + radio_result + "\n爱好:" + checkbox_result);
		}
    	
    }
    
    class resttingListened implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			stu_name.setText("");
			stu_age.setText("");
			stu_class.clearCheck();
			for(int i = 0; i < bobbyCb.length; i++) {
				bobbyCb[i].setChecked(false);
			}
			result.setText("");
		}
    	
    }
    
}

在这里插入图片描述

1.shiyan0402

string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="action_settings">Settings</string>
    
    <string name="title">图片操作</string>
    <string name="pic_content">宠物图片</string>
    <string name="pic_pre">上一张</string>
    <string name="pic_next">下一张</string>
    <string name="pic_add_alpha">增加透明度</string>
    <string name="pic_sub_alpha">降低透明度</string>

</resources>


activity_main.xml
<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: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"
    android:gravity="center_horizontal" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:textColor="#f00"
        android:textSize="20sp"/>
        
    <ImageView 
     android:id="@+id/images"
     android:layout_height="200dp"
     android:layout_width="200dp"
     android:scaleType="fitCenter"
     android:contentDescription="@string/pic_content"/>
        
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        
        <Button 
            android:id="@+id/btn_pic_pre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/pic_pre"
            style="?android:attr/button"/>
        
        <Button 
            android:id="@+id/btn_pic_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/pic_next"
            style="?android:attr/button"/>
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        
        <Button 
            android:id="@+id/btn_pic_add_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/pic_add_alpha"
            style="?android:attr/button"/>
        
        <Button 
            android:id="@+id/btn_pic_sub_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/pic_sub_alpha"
            style="?android:attr/button"/>
    </LinearLayout>
        
        
    
</LinearLayout>


存图

在这里插入图片描述

MainActivity.java
package com.example.shiyan0402;

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

public class MainActivity extends Activity implements OnClickListener {
	
	private Button picPre;
	private Button picNext;
	private Button addAlpha;
	private Button subAlpha;
	private ImageView imgView;
	private int[]pics = {R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4};
	private int picsIndex = 0;
	private int alpha = 255;

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

		picPre = (Button)findViewById(R.id.btn_pic_pre);
		picNext = (Button)findViewById(R.id.btn_pic_next);
		addAlpha = (Button)findViewById(R.id.btn_pic_add_alpha);
		subAlpha = (Button)findViewById(R.id.btn_pic_sub_alpha);
		imgView = (ImageView)findViewById(R.id.images);
		imgView.setImageResource(pics[0]);
		
		picPre.setOnClickListener(this);
		picNext.setOnClickListener(this);
		addAlpha.setOnClickListener(this);
		subAlpha.setOnClickListener(this);
	}

	@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;
	}
	
	@SuppressWarnings("deprecation")
	public void onClick(View view) {
		switch(view.getId()) {
			case R.id.btn_pic_pre:
				if(picsIndex == 0) {
					picsIndex = pics.length-1;
				}else {
					picsIndex = picsIndex - 1;
				}
				break;
			case R.id.btn_pic_next:
				if(picsIndex == pics.length-1) {
					picsIndex = 0;
				}else {
					picsIndex = picsIndex + 1;
				}
				break;
			case R.id.btn_pic_add_alpha:
				if(alpha >= 255) {
					Toast.makeText(MainActivity.this, "透明度已经达到最高,无法再增加了", Toast.LENGTH_SHORT).show();
				}else {
					alpha += 15;
					imgView.setAlpha(alpha);
				}
				break;
			case R.id.btn_pic_sub_alpha:
				if(alpha <= 0) {
					Toast.makeText(MainActivity.this, "透明度已经达到最低,无法再降低了", Toast.LENGTH_SHORT).show();
				}else {
					alpha -= 15;
					imgView.setAlpha(alpha);
				}
				break;
			default:
				break;
		}
		imgView.setImageResource(pics[picsIndex]);
	}

}

在这里插入图片描述

总结

更晚了。。。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值