一.功能操作
- 在eclipse中导入已有项目
file-Import-Android-existing android…-选择目录 - eclipse显示行号
window-preferences-general-textEditor-show line number
二.交互
1. 设置默认视图
MainActivity.java打开,onCreate函数中找setContentView(R.layout.second);
设置默认的视图(其中second是想展示的布局文件的名字)
2.设置点击事件
2.1 在onCreate函数中写findViewById(R.id.mybutton);可写为如下:
private Button btn; //根据准备的对象写变量(在onCreate函数上面定义)
btn=(Button)findViewById(R.id.mybutton); //寻找视图通过ID,默认在R文件中的id方法中有组件id,直接通过.来调用
btn.setOnClickListener(new OnClickListener() {//设置监听,点击效果
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "点我干啥?", Toast.LENGTH_LONG).show();//Toast是弹框,第二个参数是内容,第三个参数是显示时间
}
});
2.2 直接在标签内设置onClick
XML文件中写
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick"//在这里设置标签,注意名字要和下面Activity_main.java文件中的函数对应(ctrl+点名字,可以直接跳转到函数)
android:clickable="true" //有时候如果点击没有用,需要添加这一句,点击为true
android:text="哈哈" />
Activity_main.java中写
public void myClick(View v){//直接写函数(不用写在onCreate里面,独立写),注意写参数
Toast.makeText(getApplicationContext(), "666", 2).show();
System.out.println("------------777------------");//点击后不会出现,输出会出现在编译器中,(window-show view-problem-上面行的最后一个Logcat,选择左侧的All messages)
}
2.3 onCreate函数内部绑定,外部点击(优点:在有多个按钮需要调用点击的类时,方便复用)
内部写:
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new btnClick());//此处调用函数
外部写:
class btnClick implements View.OnClickListener{//注意这是一个类去实现点击监听活动
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "按钮被点击了", 2).show();
}
}
3.界面跳转 (关键词:intent 意图)
3.1 设置跳转界面Java文件
src-右键点击包-new-other-Android-android activity-添加新建Activity Name(注意,名字的首字母需要大写)
3.2 设置跳转界面代码
private TextView textView;
textView=(TextView)findViewById(R.id.textView1);
textView.setOnClickListener(new OnClickListener() {//设置监听,点击设置
public void onClick(View arg0) {
//Toast.makeText(getApplicationContext(), "点我", Toast.LENGTH_LONG).show();//Toast 显示的文本
Intent myintent=new Intent();//实例化intent对象
myintent.setClass(MainActivity.this, Second.class);//intent对象设置类的跳转,从一个类跳转到另一个类中,也就是界面的跳转
startActivity(myintent);//启动activity
}
});
4.单选按钮(RadioButton)与复选框(checkbox)
4.1 RadioGroup标签(按钮组),有属性orientation,默认为垂直,可以通过设置改为水平
<RadioGroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"//默认选中的是男性
android:text="男" />
<RadioButton
android:id="@+id/girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
4.2 设置单选按钮改变提醒监听
RadioGroup sex=(RadioGroup)findViewById(R.id.sex);//声明按钮组变量
sex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int checkId) { //注意上下的checkId要保持一致
RadioButton btn=(RadioButton)findViewById(checkId);//声明变量,存着选择的按钮
Toast.makeText(getApplicationContext(), "你选择了性别:"+btn.getText(), 2).show();//注意要加.show();,否则点击也不会有弹窗产生
}
});