1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android EditText 编辑框 获取焦点的方法

Android EditText 编辑框 获取焦点的方法

时间:2018-10-02 11:52:57

相关推荐

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="/apk/res/android"xmlns:tools="/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>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。