Android EditText 编辑框 获取焦点的方法
生活随笔
收集整理的這篇文章主要介紹了
Android EditText 编辑框 获取焦点的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
EditText 獲取焦點的方法為:setOnFocusChangeListener
下面寫一個EditText 編輯框獲取焦點之后改變背景顏色
主要代碼如下
public class MainActivity extends AppCompatActivity {private EditText editText;private LinearLayout linearLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = findViewById(R.id.edt);linearLayout = findViewById(R.id.main);editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus) {linearLayout.setBackgroundColor(Color.RED);} else {linearLayout.setBackgroundColor(Color.WHITE);}}});}
}
xml 中代碼就寫了一個編輯框如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><EditTextandroid:id="@+id/edt"android:layout_width="match_parent"android:layout_height="100dp"android:hint="點擊編輯框獲取焦點改變背景顏色" /></LinearLayout>
?
總結
以上是生活随笔為你收集整理的Android EditText 编辑框 获取焦点的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 好的个性签名励志
- 下一篇: 播放此电影需要以下插件,但尚未安装: M