示例(1)按鍵和文本框監聽
阿新 • • 發佈:2018-01-30
基於 所有 click button absolute str over bool stat
參考教程:
3.1.1 基於監聽的事件處理機制
http://www.runoob.com/w3cnote/android-tutorial-listen-event-handle.html
3.5 監聽EditText的內容變化
http://www.runoob.com/w3cnote/android-tutorial-listener-edittext-change.html
過程記錄
1布局管理器
1.1添加一個布局管理器
1.2添加兩個按鍵
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dongdong.myapplication.MainActivity"> //1.1 布局管理器 <LinearLayout android:layout_width="330dp" android:layout_height="61dp" android:layout_margin="5dp" android:orientation="horizontal" tools:context=".MainActivity" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="16dp"> //1.2 添加文本框 <EditText android:id="@+id/edit_pawd" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:inputType="textPassword" /> //1.3 添加按鈕 按鈕改變 密碼顯示和隱藏 <Button android:id="@+id/btnChange" android:layout_width="3dp" android:layout_height="48dp" android:layout_weight="1" android:text="密碼可見" /> // 1.4 添加按鈕 按鈕單擊 輸出一句話 <Button android:id="@+id/btnshow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="94dp" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
2 主函數
2.0 導入包
2.1 聲明變量
- private EditText edit_pawd; // 文本框
- private Button btnChange; // 按鍵1
- private boolean flag = false; //判斷
- private Button btnshow; //按鍵2
2.2 綁定變量與實際按鍵
- edit_pawd = (EditText) findViewById(R.id.edit_pawd);
- btnChange = (Button) findViewById(R.id.btnChange);
- btnshow = (Button) findViewById(R.id.btnshow);
2.3 綁定按鍵的方法
第一種模式 1)直接用匿名內部類
- 平時最常用的一種:直接setXxxListener後,重寫裏面的方法即可; 通常是臨時使用一次,復用性不高!
btnChange.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { //按鍵事件觸發 } });
第二種模式 2)使用內部類
- 和上面的匿名內部類不同哦! 使用優點:可以在該類中進行復用,可直接訪問外部類的所有界面組件!
先寫方法
class BtnClickListener implements View.OnClickListener { public void onClick(View v) { // 按鍵單擊動作 } }
然後new出來
btnshow.setOnClickListener(new BtnClickListener());
註意:方法要在調用前出現
package com.example.dongdong.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.app.Activity; public class MainActivity extends AppCompatActivity { private EditText edit_pawd; // 文本框 private Button btnChange; // 按鍵 private boolean flag = false; //判斷 private Button btnshow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit_pawd = (EditText) findViewById(R.id.edit_pawd); edit_pawd.setHorizontallyScrolling(true); //設置EditText不換行 btnChange = (Button) findViewById(R.id.btnChange); btnChange.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(flag == true){ //設置文本框隱藏模式 edit_pawd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); flag = false; btnChange.setText("密碼隱藏"); }else{ //設置文本框可見模式 edit_pawd.setTransformationMethod(PasswordTransformationMethod.getInstance()); flag = true; btnChange.setText("密碼可見"); } } }); //定義一個內部類,實現View.OnClickListener接口,並重寫onClick()方法 class BtnClickListener implements View.OnClickListener { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "按鈕被點擊了", Toast.LENGTH_SHORT).show(); } } btnshow = (Button) findViewById(R.id.btnshow); //直接new一個內部類對象作為參數 btnshow.setOnClickListener(new BtnClickListener()); } }
示例(1)按鍵和文本框監聽