预期目标:如下图,输入框中每输入一个字符,下面的TextView可以迅速的显示出来输入框中的内容。
1.第一种方法:使用 setOnKeyListener(),不过这种方式只能监听硬键盘事件。
- edittext.setOnKeyListener(new View.OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- textview.setText(edittext.getText());
- return false;
- }
- });
- edittext.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- textview.setText(edittext.getText());
- }
-
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
-
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- private void setEditable(EditText mEdit, int maxLength, boolean value) {
- if (value) {
- mEdit.setFilters(new InputFilter[] { new MyEditFilter(maxLength) });
- mEdit.setCursorVisible(true);
- mEdit.setFocusableInTouchMode(true);
- mEdit.requestFocus();
- }
- else {
- mEdit.setFilters(new InputFilter[] { new InputFilter() {
- @Override
- public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
- return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
- }
- } });
- mEdit.setCursorVisible(false);
- mEdit.setFocusableInTouchMode(false);
- mEdit.clearFocus();
- }
- }
Android获取文字的宽度
- Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mTextPaint.setColor(Color.WHITE);
-
- // Define the string.
- String displayText = "Hello World!";
-
- // Measure the width of the text string.
- float textWidth = mTextPaint.measureText(displayText);
android ArrayAdapter的使用
写一个XML用于显示ListView。
list1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/checklv1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- private String[] array1= {"aaa", "bbb", "ccc", "ddd"};
- private ListView lv1;
- lv1 = (ListView) findViewById(R.id.lv1);
- ArrayAdapter adapter1=new ArrayAdapter (showinfo.this,R.layout.list1,array1);
- lv1.setAdapter(adapter1);
----------------------------------------------------------------------------------------------
Android TextView无焦点滚动显示第一种:
- Animation ani = new TranslateAnimation(310f, -400f, 0.0f, 0.0f);
- ani.setDuration(10000);
- ani.setRepeatCount(500);
- ani.setRepeatMode(1);
- TextView textView =(TextView) this.findViewById(R.id.text);
- textView .startAnimation(ani);
- <TextView
- android:id="@+id/TextView02"
- android:singleLine="true"
- android:scrollHorizontally="true"
- android:ellipsize="marquee"
- android:marqueeRepeatLimit="marquee_forever"
- android:textSize="16px"
- android:focusable="true"
- android:textColor="@color/gray"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- nextsong = (TextView) findViewById(R.id.TextView02);
- nextsong.setSelected(true);