

点击事件,点击吐司(Toast),匿名内部类实现
source link: http://www.androidchina.net/8888.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1、按钮点击事件
当点击按钮后出现效果或结果就是点击事件,首先先看效果图,点击后吐司在屏幕上、
代码如下:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
android.support.constraint.ConstraintLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
Button
android:id
=
"@+id/btn"
android:layout_width
=
"100dp"
android:layout_height
=
"40dp"
android:text
=
"点击"
/>
</
android.support.constraint.ConstraintLayout
>
Java:
package
com.example.zy.mystudy;
import
android.app.Activity;
import
android.content.DialogInterface;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity {
private
Button button;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
Toast.makeText(MainActivity.
this
,
"匿名内部类方式"
, Toast.LENGTH_SHORT).show();
}
}
);
}
}
这样是通过了匿名内部类方式实现了点击按钮吐司
=====================================================================
同时,你也可以这样写:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
android:orientation
=
"vertical"
>
<
Button
android:id
=
"@+id/btn1"
android:layout_width
=
"100dp"
android:layout_height
=
"40dp"
android:text
=
"点击1"
/>
<
Button
android:id
=
"@+id/btn2"
android:layout_width
=
"100dp"
android:layout_height
=
"40dp"
android:text
=
"点击2"
/>
</
LinearLayout
>
Java:
package
com.example.zy.mystudy;
import
android.app.Activity;
import
android.content.DialogInterface;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity {
private
Button button1;
private
Button button2;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.btn1);
button1.setOnClickListener(onClickListener);
button2 = (Button)findViewById(R.id.btn2);
button2.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener =
new
View.OnClickListener(){
@Override
public
void
onClick(View view) {
switch
(view.getId()){
case
R.id.btn1:
Toast.makeText(MainActivity.
this
,
"点击事件1"
,Toast.LENGTH_SHORT).show();
break
;
case
R.id.btn2:
Toast.makeText(MainActivity.
this
,
"点击事件2"
,Toast.LENGTH_SHORT).show();
break
;
}
}
};
}
这样写可以避免在创建时多次占用资源
转载请注明:Android开发中文站 » 点击事件,点击吐司(Toast),匿名内部类实现
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK