3中註冊監聽方式比較android
1.匿名內部類app
須要獲取控件對象,使用變量不方便;適用於單個事件ide
2.實現接口this
須要獲取控件對象,使用變量方便;適用於於多個事件spa
3.設置onClick屬性xml
無需得到控件對象,使用變量方便;不便維護。對象
1匿名內部類 代碼實現接口
Button btn_show;
btn_show = (Button) findViewById(R.id.btn_show); //強轉對象 btn_show.setOnClickListener(this);
//註冊監聽器 btn_show.setOnClickListener(new View.OnClickListener() { //匿名內部類 @Override public void onClick(View view) { //響應 String text = "你點擊了按鈕"; Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show(); Log.i("sp1",text); } });
2 實現接口 代碼實現
public class MainActivity extends AppCompatActivity implements View.OnClickListener
Button btn_show;
btn_show = (Button) findViewById(R.id.btn_show); //強轉對象 btn_show.setOnClickListener(this);
@Override public void onClick(View view) { //實現接口 //響應 String text = "你點擊了按鈕!!!"; Toast.makeText(this,text,Toast.LENGTH_SHORT).show(); Log.i("sp1",text); }
3.設置onClick屬性 在xml main.xml文件中的Butten中添加一行代碼
android:onClick="btnClick"
public void btnClick(View v){ //自定義方法 String text = "你點擊了按鈕!!?"; Toast.makeText(this,text,Toast.LENGTH_SHORT).show(); Log.i("sp1",text); }
推薦使用的話是前兩種。
內部定義
運行截圖
<Button android:id="@+id/loginbutten" android:layout_marginTop="90dp" android:text="點我" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp"/> <EditText android:id="@+id/username" android:hint="用戶名" android:inputType="textPassword" android:layout_width="180dp" android:layout_height="wrap_content" android:textColorHint="#0f0" android:layout_marginLeft="30dp" /> <EditText android:id="@+id/password" android:hint="密碼" android:inputType="textPassword" android:textColorHint="#0f0" android:layout_width="180dp" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_marginLeft="30dp" />
public class MainActivity extends AppCompatActivity { private Button loginbutten; private TextView username; private TextView password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginbutten = (Button) findViewById(R.id.loginbutten); username = (TextView) findViewById(R.id.username); password = (TextView) findViewById(R.id.password); loginbutten.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View view) { String msg = "用戶名"+username.getText()+"密碼是"+password.getText(); Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); } }); }
內部定義2
package com.example.deligence.myapp1; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button loginbutten; private TextView username; private TextView password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginbutten = (Button) findViewById(R.id.loginbutten); username = (TextView) findViewById(R.id.username); password = (TextView) findViewById(R.id.password); Button.OnClickListener listener = new Button.OnClickListener(){ @Override public void onClick(View view) { String msg = "用戶名"+username.getText()+"密碼是"+password.getText(); Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); } }; loginbutten.setOnClickListener(listener); } }