废话不多说,直接上代码供参考:
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) {
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) {
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){
index = images.length-1;
iv.setImageResource(images[index]);
}else{
iv.setImageResource(images[--index]);
}
}
private void nextImage(){
if(index == images.length-1){
index = 0;
iv.setImageResource(images[index]);
}else{
iv.setImageResource(images[++index]);
}
}
}
附图:
|