您的位置:首页 > 其它

Button的3种单击事件监听器设置

2015-09-02 10:17 295 查看
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_text1"
android:onClick="onClickMethod"
android:text="使用onclick属性" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:text="使用View.OnClickListener方法" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:text="使用按钮的OnClickListener" />


public class MainActivity extends Activity implements View.OnClickListener {
<span style="white-space:pre">	</span>bt5 = (Button) findViewById(R.id.button5);
bt6 = (Button) findViewById(R.id.button6);
bt5.setOnClickListener(this);
bt6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "使用按钮的OnClickListener", Toast.LENGTH_SHORT).show();
}
});
}
public void onClickMethod(View v) {
Toast.makeText(this, "使用onclick属性", Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button5:
Toast.makeText(this, "使用View.OnClickListener方法", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: