Android-常用基础控件-单选(复选)按钮-图片视图

单选按钮和复选按钮的使用示例

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
  
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView 
		    android:layout_width="100px"
        	android:layout_height="wrap_content"
        	android:height="50px"
            android:text="   姓 名 : "
		    />
       <EditText    
	        android:id="@+id/et_name"
	        android:layout_width="300px"
	       	android:layout_height="wrap_content"
	        android:inputType="text"  
           />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView 
		    android:layout_width="100px"
        	android:layout_height="wrap_content"
        	android:height="50px"
            android:text="   年 龄 : "
		    />
       <EditText 
	        android:id="@+id/et_age"
	        android:layout_width="300px"
	       	android:layout_height="wrap_content"
	        android:inputType="text"  
           />  
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView 
		    android:layout_width="100px"
        	android:layout_height="wrap_content"
        	android:height="50px"
            android:text="   专 业 : "
		    />
       <RadioGroup 
           android:id="@+id/radioGroup1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           >
           <RadioButton 
               android:id="@+id/rb1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="计算机科学与技术"
               />
           <RadioButton 
               android:id="@+id/rb2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="软件工程"
               />
           <RadioButton 
               android:id="@+id/rb3"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="网络工程"
               />
           
           
       </RadioGroup>
    </LinearLayout>
    
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView 
		    android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:height="50px"
            android:text="学生爱好:"
		    />
        <GridLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="3"
            android:id="@+id/g1_hobby"
            >
              <CheckBox 
		           android:id="@+id/cb1"
		           android:layout_width="wrap_content"
		           android:layout_height="wrap_content"
		           android:text="阅读"
		           />
		       <CheckBox 
		           android:id="@+id/cb2"
		           android:layout_width="wrap_content"
		           android:layout_height="wrap_content"
		           android:text="旅游"
		           />
		       <CheckBox 
		           android:id="@+id/cb3"
		           android:layout_width="wrap_content"
		           android:layout_height="wrap_content"
		           android:text="发呆"
		        />
		        <CheckBox 
		           android:id="@+id/cb4"
		           android:layout_width="wrap_content"
		           android:layout_height="wrap_content"
		           android:text="唱歌"
		           />
		       <CheckBox 
		           android:id="@+id/cb5"
		           android:layout_width="wrap_content"
		           android:layout_height="wrap_content"
		           android:text="编程"
		           />
		       <CheckBox 
		           android:id="@+id/cb6"
		           android:layout_width="wrap_content"
		           android:layout_height="wrap_content"
		           android:text="运动"
           />
            
            
        </GridLayout>
       
    </LinearLayout>
    

    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center">
        <Button 
            android:id="@+id/bt_login"
		    android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="录入"
        	android:onClick="onClickLogin"
		    />
       <Button 
           android:id="@+id/bt_reset"          
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
           android:text="重置"
           android:onClick="onClickReset"
           />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
        <TextView 
            android:id="@+id/tv_show"
		    android:layout_width="wrap_content"
        	android:layout_height="wrap_content"

            android:text="录入信息:"
		    />
      </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.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View. OnClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

	//声明
	
	RadioGroup zy;
	
	EditText etname,etage;
	TextView tvshow;
	
	CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
	CheckBox cb[]=new CheckBox[6];
	
	Button bt1,bt2;
	
	GridLayout g1;
	
	String name,age,specialty,shobby;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//获取控件对象
		bt1 = (Button) findViewById(R.id.bt_login);
		bt2 = (Button) findViewById(R.id.bt_reset);
		//复选框
		cb[0] = (CheckBox) findViewById(R.id.cb1);
		cb[1] = (CheckBox) findViewById(R.id.cb2);
		cb[2] = (CheckBox) findViewById(R.id.cb3);
		cb[3] = (CheckBox) findViewById(R.id.cb4);
		cb[4] = (CheckBox) findViewById(R.id.cb5);
		cb[5] = (CheckBox) findViewById(R.id.cb6);
		

		g1 = (GridLayout) findViewById(R.id.g1_hobby);
		
		etname = (EditText) findViewById(R.id.et_name);
		etage = (EditText) findViewById(R.id.et_age);
		
		tvshow = (TextView) findViewById(R.id.tv_show);
		
		
		zy = (RadioGroup) findViewById(R.id.radioGroup1);
		
		
		zy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				RadioButton r = (RadioButton) zy.getChildAt(checkedId);
				
			}
		});
		
		
		
		bt1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				//获取复选框中被选选项的文本信息
//				for(int i=0;i<cb.length;i++) {
//					if(cb[i].isChecked()) {
//						shobby+=cb[i];
//					}
//				}
								
				shobby="";
				//复选框对象多时使用
				//对布局管理器进行内部控件对象的遍历,i用于表示子元素在布局管理的下标
				for(int i=0;i<g1.getChildCount() ;i++) {
					CheckBox cbs = (CheckBox) g1.getChildAt(i);
					//判断状态
					if(cb[i].isChecked()) {
						shobby+=cbs.getText().toString()+"  ";
					}
				}
				for(int i=0;i<zy.getChildCount();i++) {
					RadioButton r = (RadioButton) zy.getChildAt(i);
					if(r.isChecked()) {
						specialty = r.getText().toString();
					}
				}
								
				name = etname.getText().toString();
				age = etage.getText().toString();
										
				//显示
				tvshow.append("\n姓名:"+name+"\n年龄:"+age+"\n专业:"+specialty+"\n爱好:"+shobby);
								
			}
		});
		
		//重置
		bt2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub								
				etname.setText("");
				etage.setText("");
				
				for(int i=0;i<zy.getChildCount();i++) {
					RadioButton r = (RadioButton) zy.getChildAt(i);
					if(r.isChecked()) {
						r.setChecked(false);
					}
				}
				
				for(int i=0;i<g1.getChildCount() ;i++) {
					CheckBox cbs = (CheckBox) g1.getChildAt(i);
					cbs.setChecked(false);
				}
				
				tvshow.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;
	}

}

运行:

 

图片域应用

布局

 

图片资源放置

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:orientation="vertical"
    android:gravity="center"
    >
		<ImageView 
		    android:id="@+id/iv"
		    android:layout_width="400px"
			android:layout_height="300px"
			android:src="@drawable/image2"
			android:layout_gravity="center"
		    />
			<Button
		        android:id="@+id/bt_next"
		        android:layout_width="200sp"
		        android:layout_height="wrap_content"
		        android:text="下一张" />

		    <Button
		        android:id="@+id/bt_back"
		        android:layout_width="200sp"
		        android:layout_height="wrap_content"
		        android:text="上一张" />

		    <Button
		        android:id="@+id/bt_down"
		        android:layout_width="200sp"
		        android:layout_height="wrap_content"
		        android:text="透明度降低" />

		    <Button
		        android:id="@+id/bt_up"
		        android:layout_width="200sp"
		        android:layout_height="wrap_content"
		        android:text="透明度增加" />		


		    
		
    
</LinearLayout>

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;

public class MainActivity extends Activity {

	//声明
	ImageView iv;
	Button btnext,btback,btup,btdown ;
	
	int image[]= {R.drawable.image1,R.drawable.image2,R.drawable.image3,
			R.drawable.image4,R.drawable.image5,R.drawable.image6};
	
	//图片透明度
	int alpha=255;
	
	int position=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//获取控件对象
		iv = (ImageView) findViewById(R.id.iv);
		
		btnext = (Button) findViewById(R.id.bt_next);
		btback = (Button) findViewById(R.id.bt_back);
		btup = (Button) findViewById(R.id.bt_up);
		btdown = (Button) findViewById(R.id.bt_down);
		
		//设置监听器
		btnext.setOnClickListener(new ButtonListener());
		btback.setOnClickListener(new ButtonListener());
		btup.setOnClickListener(new ButtonListener());
		btdown.setOnClickListener(new ButtonListener());
		
		
		
	}
	
	class ButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.bt_next :
				position++;
				if(position == 6) {
					position = 1;
				}
				iv.setImageResource(image[position]);
				
				break;
			case R.id.bt_back :
				position--;
				if(position == -1) {
					position = 5;
				}
				iv.setImageResource(image[position]);
				
				break;
			case R.id.bt_up :
				if(alpha<255) {
					alpha += 5 ;
					iv.setAlpha(alpha);
				}
				break;
			case R.id.bt_down :
				if(alpha>0) {
					alpha -= 5 ;
					iv.setAlpha(alpha);
				}
				break;
				
				
			default:
				break;
			}
		}
		
	}
	
	

	@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;
	}




}

初始界面

点击下一张

点击透明度降低

 

点击"透明度增加"

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ML.star

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值