第一题:信息显示
linear.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"
android:padding="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20sp" />
<EditText
android:id="@+id/et1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="@string/qing"
android:singleLine="true"
android:width="60dp" />
</LinearLayout>
<!-- 年龄 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="20sp" />
<EditText
android:id="@+id/et2"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="@string/nian"
android:inputType="number"
android:singleLine="true"
android:width="60dp" />
</LinearLayout>
<!-- 专业 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="@string/spe"
android:textSize="20sp" />
<!-- 单选按钮 -->
<RadioGroup
android:id="@+id/rg"
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:checked="true"
android:text="@string/jisuanji"
android:textSize="15sp" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/ruanjian"
android:textSize="15sp" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/wangluo"
android:textSize="15sp" />
</RadioGroup>
<!-- 爱好 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="@string/hobby"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sports" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mv" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/music" />
</LinearLayout>
<!-- 按钮 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter"
android:background="#dcdcdc"
android:onClick="myClick1"/>
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset"
android:background="#dcdcdc"
android:layout_marginLeft="2dp"
android:onClick="myClick2"/>
</LinearLayout>
<TextView
android:id="@+id/xinxi"
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:text="@string/xinxi"
android:textSize="23sp"
android:background="#ccc" />
</LinearLayout>
strings.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">shiyan0401_姓名</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="xinxi">信息显示:</string>
<string name="name">姓名:</string>
<string name="age">年龄:</string>
<string name="spe">专业:</string>
<string name="jisuanji">计算机科学与技术</string>
<string name="ruanjian">软件工程</string>
<string name="wangluo">网络工程</string>
<string name="hobby">爱好:</string>
<string name="sports">体育</string>
<string name="mv">电影</string>
<string name="music">音乐</string>
<string name="enter">录入</string>
<string name="reset">重置</string>
<string name="qing">请输入姓名</string>
<string name="nian">请输入年龄</string>
</resources>
MainActivity.java文件:
package com.example.shiyan0401_ranliebao;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
Button bt1;
Button bt2;
EditText et1;
EditText et2;
TextView xinxi;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
et1 =(EditText) findViewById(R.id.et1);
et2 =(EditText) findViewById(R.id.et2);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
rg =(RadioGroup) findViewById(R.id.rg);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
xinxi = (TextView)findViewById(R.id.xinxi);
}
@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 myClick1(View view) {
String tex1 = et1.getText().toString();
String tex2 = et2.getText().toString();
String zhuanye = "";
String[] aihao=new String[3];
//获取专业
for(int i = 0; i < rg.getChildCount(); i++){
RadioButton r = (RadioButton) rg.getChildAt(i);
if(r.isChecked()){
zhuanye = r.getText().toString();
break;
}
}
//获取爱好
if (cb1.isChecked()) {
aihao[0] = cb1.getText().toString();
} else { aihao[0] = ""; }
if (cb2.isChecked()) {
aihao[1] = cb2.getText().toString();
} else { aihao[1] = ""; }
if (cb3.isChecked()) {
aihao[2] = cb3.getText().toString();
} else { aihao[2] = ""; }
xinxi.setText("姓名:" + tex1 + " " + "年龄:" + tex2 + " " +
"专业:" + zhuanye + " " + "爱好:" + aihao[0] + " " + aihao[1] + " " + aihao[2] );
}
//重置事件
public void myClick2(View view) { //重置
et1.setText("");
et2.setText("");
for(int i = 0; i < rg.getChildCount(); i++){
RadioButton r = (RadioButton) rg.getChildAt(i);
if(r.isChecked()){
r.setChecked(false);
break;
}
}
cb1.setChecked(false);
cb2.setChecked(false);
cb3.setChecked(false);
}
}
截屏显示:
第二题:图片设置:
linear.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" >
<!-- t图片 -->
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
android:src="@drawable/pic"
android:maxWidth="200dp"
android:maxHeight="200dp"
android:adjustViewBounds="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/next1"
android:background="#dcdcdc"
android:layout_margin="3dp"
android:onClick="myClick" />
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/next2"
android:background="#dcdcdc"
android:layout_margin="3dp"
android:onClick="myClick"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reduce"
android:background="#dcdcdc"
android:layout_margin="3dp"
android:onClick="myClick3"/>
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/increase"
android:background="#dcdcdc"
android:layout_margin="3dp"
android:onClick="myClick4" />
</LinearLayout>
</LinearLayout>
string文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">shiyan0402_姓名</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="next1">下一张</string>
<string name="next2">上一张</string>
<string name="reduce">透明度降低</string>
<string name="increase">透明度增加</string>
</resources>
MainActivity.java文件:
package com.example.shiyan0402_ranliebao;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button bt1;
Button bt2;
Button bt3;
Button bt4;
ImageView iv;
int number = 200;
//存储图片资源的数组
int[] photos = {R.drawable.pic, R.drawable.wife, };
//显示当前图片的索引
int photoIndex = 0;
//图片索引最大值
int maxIndex = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
bt1 = (Button)findViewById(R.id.bt1);
bt2 = (Button)findViewById(R.id.bt2);
bt3 = (Button)findViewById(R.id.bt3);
bt4 = (Button)findViewById(R.id.bt4);
iv = (ImageView)findViewById(R.id.iv);
}
@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;
}
//下一张bt1,myClick1
public void myClick(View view) {
switch (view.getId()) {
case R.id.bt1:
//如果当前图片是第一张,则上一张图片为最后一张图片
if (photoIndex == 0) {
photoIndex = maxIndex;
} else {
//否则改为上一张图片索引
photoIndex = photoIndex - 1;
}
break;
case R.id.bt2:
//如果当前图片是最后一张,则下一张图片为第一张图片
if (photoIndex == maxIndex) {
photoIndex = 0;
} else {
//否则改为下一张图片索引
photoIndex = photoIndex + 1;
}
break;
default:
break;
}
//显示图片
iv.setImageResource(photos[photoIndex]);
}
// //上一张bt2,myClick2
//public void myClick2(View view) {
//
// }
//透明度降低bt3,myClick3
@SuppressLint("NewApi")
public void myClick3(View view) {
number += 100;
iv.setImageAlpha(number);
}
//透明度增加bt4,myClick4
@SuppressLint("NewApi")
public void myClick4(View view) {
number -= 100;
iv.setImageAlpha(number);
}
}
截屏: