public class AMainActivity extends Activity {
//布局中的视图都要声明成员变量
TextView tv1;
EditText et1;
Button b1;
ImageView im;
//定义一个Boolean类型
boolean boos = true;
RadioButton c1 , c2 ,c3;
RadioGroup ff ,rgi;
//启动程序都要调用onCreat方法
@Override
protected void onCreate(Bundle savedInstanceState) {
//调用父类的oncreate方法进行一些初始化操作
super.onCreate(savedInstanceState);
//加载布局,并生成对应试图对象
setContentView(R.layout.activity_cmain);
info();
}
private void info() {
// TODO Auto-generated method stub
//获取对象
tv1 = (TextView) findViewById(R.id.tv1);
//在代码中设置内容
tv1.setText(R.string.hello_world);
//获取对象
et1=(EditText) findViewById(R.id.editText1);
//在代码中设置内容
et1.setText("123131");
//获取对象
b1 =(Button) findViewById(R.id.b1);
//在代码中设置内容
b1.setText("电话");
//添加点击监听
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//得到内容
String ts=b1.getText().toString().trim();
}
});
//获取对象
im = (ImageView) findViewById(R.id.imageView1);
//加点击监听
im.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(boos){
//在代码中设置背景图片
im.setBackgroundResource(android.R.drawable.arrow_down_float);
//在代码中设置前景图片
im.setImageResource(R.drawable.b8);
boos =false;
}else{
//在代码中设置背景图片
im.setBackgroundResource(R.drawable.b9);
//在代码中设置前景图片
im.setImageResource(R.drawable.bu2);
//如果为true就会调用if里面的方法如果不为true则调用else里面的方法
boos =true;
}
}
});
//男女
ff = (RadioGroup) findViewById(R.id.rg);
//添加监听
ff.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//获取checkedId方法里面的内容
RadioButton btr = (RadioButton) findViewById(checkedId);
//获取内容
String st = btr.getText().toString();
// Toast tt = new Toast(getApplicationContext());
// tt.setGravity(Gravity.AXIS_X_SHIFT,0,0);
// tt.setDuration(5000);
// tt.show();
//土司
Toast.makeText(getApplicationContext(), st, 3).show();
}
});
//乒乓
//获取id
rgi = (RadioGroup) findViewById(R.id.rgi);
//添加监听
rgi.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//获取checkedId方法里面的内容
RadioButton btr = (RadioButton) findViewById(checkedId);
//获取内容
String st = btr.getText().toString();
// Toast tt = new Toast(getApplicationContext());
// tt.setGravity(Gravity.AXIS_X_SHIFT,0,0);
// tt.setDuration(5000);
// tt.show();
//土司
Toast.makeText(getApplicationContext(), st, 3).show();
}
});
//获取id
// c1 =(RadioButton) findViewById(R.id.ai);
// c2 =(RadioButton) findViewById(R.id.ai1);
// c3 =(RadioButton) findViewById(R.id.hao);
// //选中状态改变的监听
// c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//选中状态改变的监听
// if(isChecked){
// Toast.makeText(getApplicationContext(), "选中了足球", 2).show();
// }else{
// Toast.makeText(getApplicationContext(), "选中了足球", 2).show();
// }
// }
// });
//选中状态改变的监听
// c2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//选中状态改变的监听
// if(isChecked){
// Toast.makeText(getApplicationContext(), "选中了篮球", 2).show();
// }else{
// Toast.makeText(getApplicationContext(), "选中了篮球", 2).show();
// }
// }
// });
//选中状态改变的监听
// c3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//选中状态改变的监听
// if(isChecked){
// Toast.makeText(getApplicationContext(), "选中了乒乓球", 2).show();
// }else{
// Toast.makeText(getApplicationContext(), "选中了乒乓球", 2).show();
// }
// }
// });
}
// public void confirm(View v){
// //比String更强
// StringBuffer st = new StringBuffer();
// if (c1.isChecked()) {
// st.append(c1.getText().toString()).append("");
//
// }
// if (c2.isChecked()) {
// st.append(c2.getText().toString()).append("");
//
// }
// if (c3.isChecked()) {
// st.append(c3.getText().toString()).append("");
//
// }
// Toast.makeText(getApplicationContext(), st.toString(), 2).show();
//
// }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<!-- android:id="@+id/button1" 通过他在Mainactivity当中 调用findViewById进行一些初始化
android:text="下载中" 设置buttton运行时显示的名字 他设置在String.xml文件中,也可以直接西如,直接写入会占用内存,多的话不建议使用
android:layout_width="match_parent"
android:layout_height="wrap_content" 设置控件的宽和高
android:textColor="#ff0000" 设置字体的颜色
android:background="#999999"设置背景颜色
android:hint="请输入电话号" 设置EditText在没输入文字的时候默认为 (请输入电话号)-->
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20dp"
android:background="#999999"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号"
android:inputType="phone">
<requestFocus android:layout_width="match_parent" />
</EditText>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bu2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:" />
<RadioGroup
android:id="@+id/rgi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/ai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":足球" />
<RadioButton
android:id="@+id/ai1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":篮球" />
<RadioButton
android:id="@+id/hao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":乒乓球" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="confirm"
android:text="确定" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/sb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啦啦啦" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
package com.beicai.test04_exception;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/*
常见的异常:
1. NullPointerException
原因:调用方法的属性,但是对象为null
2. ClassCastException
原因:类型转换异常
3. ActivityNotFoundException:
原因:没有在清单文件中注册,或注册不明确
基本常见异常的一般分析步骤:
1. 在laocat从下向上找,尽量找到caused by(会显示那种异常导致的)
2. 找出异常的类及行号,双击进入对应的行
*/
public class MainActivity extends Activity {
private Button btn_main_start;
private Button btn_main_start2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_main_start = (Button) findViewById(R.id.btn_main_start);
btn_main_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
SecondActivity.class));
}
});
btn_main_start2 = (Button) findViewById(R.id.tv_main_start2);
btn_main_start2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
SecondActivity.class));
}
});
}
}
//布局中的视图都要声明成员变量
TextView tv1;
EditText et1;
Button b1;
ImageView im;
//定义一个Boolean类型
boolean boos = true;
RadioButton c1 , c2 ,c3;
RadioGroup ff ,rgi;
//启动程序都要调用onCreat方法
@Override
protected void onCreate(Bundle savedInstanceState) {
//调用父类的oncreate方法进行一些初始化操作
super.onCreate(savedInstanceState);
//加载布局,并生成对应试图对象
setContentView(R.layout.activity_cmain);
info();
}
private void info() {
// TODO Auto-generated method stub
//获取对象
tv1 = (TextView) findViewById(R.id.tv1);
//在代码中设置内容
tv1.setText(R.string.hello_world);
//获取对象
et1=(EditText) findViewById(R.id.editText1);
//在代码中设置内容
et1.setText("123131");
//获取对象
b1 =(Button) findViewById(R.id.b1);
//在代码中设置内容
b1.setText("电话");
//添加点击监听
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//得到内容
String ts=b1.getText().toString().trim();
}
});
//获取对象
im = (ImageView) findViewById(R.id.imageView1);
//加点击监听
im.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(boos){
//在代码中设置背景图片
im.setBackgroundResource(android.R.drawable.arrow_down_float);
//在代码中设置前景图片
im.setImageResource(R.drawable.b8);
boos =false;
}else{
//在代码中设置背景图片
im.setBackgroundResource(R.drawable.b9);
//在代码中设置前景图片
im.setImageResource(R.drawable.bu2);
//如果为true就会调用if里面的方法如果不为true则调用else里面的方法
boos =true;
}
}
});
//男女
ff = (RadioGroup) findViewById(R.id.rg);
//添加监听
ff.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//获取checkedId方法里面的内容
RadioButton btr = (RadioButton) findViewById(checkedId);
//获取内容
String st = btr.getText().toString();
// Toast tt = new Toast(getApplicationContext());
// tt.setGravity(Gravity.AXIS_X_SHIFT,0,0);
// tt.setDuration(5000);
// tt.show();
//土司
Toast.makeText(getApplicationContext(), st, 3).show();
}
});
//乒乓
//获取id
rgi = (RadioGroup) findViewById(R.id.rgi);
//添加监听
rgi.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//获取checkedId方法里面的内容
RadioButton btr = (RadioButton) findViewById(checkedId);
//获取内容
String st = btr.getText().toString();
// Toast tt = new Toast(getApplicationContext());
// tt.setGravity(Gravity.AXIS_X_SHIFT,0,0);
// tt.setDuration(5000);
// tt.show();
//土司
Toast.makeText(getApplicationContext(), st, 3).show();
}
});
//获取id
// c1 =(RadioButton) findViewById(R.id.ai);
// c2 =(RadioButton) findViewById(R.id.ai1);
// c3 =(RadioButton) findViewById(R.id.hao);
// //选中状态改变的监听
// c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//选中状态改变的监听
// if(isChecked){
// Toast.makeText(getApplicationContext(), "选中了足球", 2).show();
// }else{
// Toast.makeText(getApplicationContext(), "选中了足球", 2).show();
// }
// }
// });
//选中状态改变的监听
// c2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//选中状态改变的监听
// if(isChecked){
// Toast.makeText(getApplicationContext(), "选中了篮球", 2).show();
// }else{
// Toast.makeText(getApplicationContext(), "选中了篮球", 2).show();
// }
// }
// });
//选中状态改变的监听
// c3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//选中状态改变的监听
// if(isChecked){
// Toast.makeText(getApplicationContext(), "选中了乒乓球", 2).show();
// }else{
// Toast.makeText(getApplicationContext(), "选中了乒乓球", 2).show();
// }
// }
// });
}
// public void confirm(View v){
// //比String更强
// StringBuffer st = new StringBuffer();
// if (c1.isChecked()) {
// st.append(c1.getText().toString()).append("");
//
// }
// if (c2.isChecked()) {
// st.append(c2.getText().toString()).append("");
//
// }
// if (c3.isChecked()) {
// st.append(c3.getText().toString()).append("");
//
// }
// Toast.makeText(getApplicationContext(), st.toString(), 2).show();
//
// }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<!-- android:id="@+id/button1" 通过他在Mainactivity当中 调用findViewById进行一些初始化
android:text="下载中" 设置buttton运行时显示的名字 他设置在String.xml文件中,也可以直接西如,直接写入会占用内存,多的话不建议使用
android:layout_width="match_parent"
android:layout_height="wrap_content" 设置控件的宽和高
android:textColor="#ff0000" 设置字体的颜色
android:background="#999999"设置背景颜色
android:hint="请输入电话号" 设置EditText在没输入文字的时候默认为 (请输入电话号)-->
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20dp"
android:background="#999999"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号"
android:inputType="phone">
<requestFocus android:layout_width="match_parent" />
</EditText>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bu2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:" />
<RadioGroup
android:id="@+id/rgi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/ai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":足球" />
<RadioButton
android:id="@+id/ai1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":篮球" />
<RadioButton
android:id="@+id/hao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":乒乓球" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="confirm"
android:text="确定" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/sb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啦啦啦" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
package com.beicai.test04_exception;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/*
常见的异常:
1. NullPointerException
原因:调用方法的属性,但是对象为null
2. ClassCastException
原因:类型转换异常
3. ActivityNotFoundException:
原因:没有在清单文件中注册,或注册不明确
基本常见异常的一般分析步骤:
1. 在laocat从下向上找,尽量找到caused by(会显示那种异常导致的)
2. 找出异常的类及行号,双击进入对应的行
*/
public class MainActivity extends Activity {
private Button btn_main_start;
private Button btn_main_start2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_main_start = (Button) findViewById(R.id.btn_main_start);
btn_main_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
SecondActivity.class));
}
});
btn_main_start2 = (Button) findViewById(R.id.tv_main_start2);
btn_main_start2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
SecondActivity.class));
}
});
}
}