1.在xml中设置按键OnClick绑定的函数
<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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonBeClicked"
android:text="按键1" />
</RelativeLayout>
package com.example.bha.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonBeClicked(View v)
{
System.out.println("按键1被按下");
}
}
要在JAVA代码中找到按键,定义buttonBeClicked函数,public的目的是希望扩大这个函数的可见度;输入View参数,当输入参数之后会显示报错,这时按下ctrl+shift+o进行导包。
运行效果:
当按键被按下时,在eclipse中的Logcat显示参数信息
如果没有显示logcat怎么找
Window->Show View->Other->Android->logcat
点击OK即可
当有多个按键时怎么实现
用拖拽的方式生成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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonBeClicked"
android:text="按键1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="40dp"
android:onClick="buttonBeClicked"
android:text="按键2" />
</RelativeLayout>
在java代码中打印调试信息
package com.example.bha.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonBeClicked(View v)
{
switch(v.getId())
{
case R.id.button1:
System.out.println("按键1被按下");
break;
case R.id.button2:
System.out.println("按键2被按下");
break;
}
}
}
运行效果
logcat调试信息
在app中显示当按键被按下时,所打印的调试信息
Toast.makeText(this, "按键1被按下", 0).show();
this代表调用指针,指向MainActivity,中间代表显示的内容,最后代表显示的时常,默认为0
.show()可在app中显示
package com.example.bha.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonBeClicked(View v)
{
switch(v.getId())
{
case R.id.button1:
System.out.println("按键1被按下");
Toast.makeText(this, "按键1被按下", 0).show();
break;
case R.id.button2:
System.out.println("按键2被按下");
Toast.makeText(this, "按键2被按下", 0).show();
break;
}
}
}
运行效果
注:当按键过多时,如何区分按键,用getId来区分所绑定的ID
——@上官可编程