UI的定义:
由View和ViewGroup组成
View的ApI结构
UI的组成
理解UI事件:
当用户通过手指触摸ui时,系统会自动创建对应的Event对象
Android中提供了许多种方式拦截处理不同类型的事件
视图本身就是可以处理发生在该视图上的事件
常用组件:
TextvView:文本视图
:

Button: 按钮
ImageView: 图片视图
测试用例:
代码如下:
一.主界面
初始化
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()) {//简单的
case R.id.button1:
startActivity(new Intent(this,SimpleComponentActivity.class));
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
}
简单的Component
对RadioButto设置
rg_sex=(RadioGroup)findViewById(R.id.rg_sex);//获取Id
rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//设置监听
public void onCheckedChanged(RadioGroup group, int checkedId) {
//checkedId就是RadioButton的id
RadioButton rb = (RadioButton) findViewById(checkedId);
String sex = rb.getText().toString();//得到文本
Toast.makeText(SimpleComponentActivity.this, sex, Toast.LENGTH_SHORT).show();
//同理都获取ID设置监听
tv_simple_msg=(TextView)findViewById(R.id.tv_simple_msg);
et_simple=(EditText)findViewById(R.id.et_simple);
bt_simple_submit=(Button)findViewById(R.id.bt_simple_submit);
//对图片视图设置监听
iv_simple_play=(ImageView) findViewById(R.id.iv_simple_play);
iv_simple_play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (status) {
//设置背景图片
iv_simple_play.setBackgroundResource(R.drawable.ic_launcher);
//系统自带
iv_simple_play.setBackgroundResource(android.R.drawable.alert_dark_frame);
//设置前景图片
iv_simple_play.setImageResource(android.R.drawable.ic_media_pause);
status =false;
}else{
iv_simple_play.setBackgroundResource(android.R.drawable.alert_light_frame);
//设置前景图片
iv_simple_play.setImageResource(android.R.drawable.ic_media_play);
status =true;
}
}
});
//对选框获取Id设置 选择监听
cb_basketball=(CheckBox) findViewById(R.id.cb_basketball);
textView2=(CheckBox) findViewById(R.id.textView2);
cb_pingpong=(CheckBox) findViewById(R.id.cb_pingpong);
//选中状态改变的监听 我们对篮球设置 其他控件同理
cb_basketball.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(SimpleComponentActivity.this, "选中篮球", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SimpleComponentActivity.this, "未选中篮球", Toast.LENGTH_SHORT).show();
}
}
});
//是否选中 控件
public void confirm(View v){
StringBuffer sb = new StringBuffer();
if (cb_basketball.isChecked()) {
sb.append(cb_basketball.getText().toString().trim());
}
if (textView2.isChecked()) {
sb.append(textView2.getText().toString().trim());
}
if (cb_pingpong.isChecked()) {
sb.append(cb_pingpong.getText().toString().trim());
}
}
<!--
scaleType="fitXY"
指定缩放类型
如 layout_width=100dp
layout_height=80dp
android:layout_width="match_parent" 全屏(qq登录画面)
android:layout_height="match_parent"
-->
出现的异常 :
NullPointerException
原因:调用对象的方法、属性,但是对象为Null
ClassCastException
原因: 类型转换异常
ActivityNotFoundException
原因; ,没有在清单文件中注册Activity.或注册不正确
基本常见一异常的解决分析步骤:
在Logcat中从下往上找,尽量找到Caused by(会显示异常原因)
找出异常的类及 行号 双击进入对用的行