Advertisement
kennywyland

Android EditText Stupidity

Aug 2nd, 2017
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. package com.inadaydevelopment.myapp;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseAdapter;
  10. import android.widget.EditText;
  11. import android.widget.ListView;
  12. import android.widget.TextView;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.  
  22.         ListView listView = (ListView) findViewById(android.R.id.list);
  23.         listView.setAdapter(new MyAdapter(getLayoutInflater()));
  24.     }
  25.  
  26.     public class MyAdapter extends BaseAdapter {
  27.  
  28.         private LayoutInflater layoutInflater;
  29.  
  30.         private final int minDeltaForReFocus = 300;           // threshold in ms
  31.         private long focusTime = 0;                 // time of last touch
  32.         private View focusTarget = null;
  33.  
  34.         View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
  35.             @Override
  36.             public void onFocusChange(View view, boolean hasFocus) {
  37.                 long t = System.currentTimeMillis();
  38.                 long delta = t - focusTime;
  39.                 if (hasFocus) {     // gained focus
  40.                     if (delta > minDeltaForReFocus) {
  41.                         focusTime = t;
  42.                         focusTarget = view;
  43.                     }
  44.                 }
  45.                 else {              // lost focus
  46.                     if (delta <= minDeltaForReFocus &&  view == focusTarget) {
  47.                         focusTarget.post(new Runnable() {   // reset focus to target
  48.                             public void run() {
  49.                                 Log.d("BA", "requesting focus");
  50.                                 focusTarget.requestFocus();
  51.                             }
  52.                         });
  53.                     }
  54.                 }
  55.             }
  56.         };
  57.  
  58.         public MyAdapter(LayoutInflater inflater) {
  59.             this.layoutInflater = inflater;
  60.         }
  61.  
  62.         @Override
  63.         public int getCount() {
  64.             return 3;
  65.         }
  66.  
  67.         @Override
  68.         public Object getItem(int position) {
  69.             return position;
  70.         }
  71.  
  72.         @Override
  73.         public long getItemId(int position) {
  74.             return position;
  75.         }
  76.  
  77.         @Override
  78.         public View getView(int position, View convertView, ViewGroup parent) {
  79.  
  80.             if (convertView == null) {
  81.                 convertView = layoutInflater.inflate(R.layout.cell_textfield, parent, false);
  82.             }
  83.  
  84.             TextView label = (TextView) convertView.findViewById(R.id.textview1);
  85.             EditText textfield = (EditText) convertView.findViewById(R.id.textview2);
  86.  
  87.             String text = String.format("Row %d", position);
  88.             label.setText(text);
  89.             textfield.setText(text);
  90.             textfield.setOnFocusChangeListener(this.onFocusChangeListener);
  91.  
  92.             return convertView;
  93.         }
  94.     }
  95. }
  96.  
  97. // ----------- R.layout.activity_main ---------------
  98.  
  99. <?xml version="1.0" encoding="utf-8"?>
  100. <android.support.constraint.ConstraintLayout
  101.     xmlns:android="http://schemas.android.com/apk/res/android"
  102.     xmlns:app="http://schemas.android.com/apk/res-auto"
  103.     xmlns:tools="http://schemas.android.com/tools"
  104.     android:layout_width="match_parent"
  105.     android:layout_height="match_parent"
  106.     tools:context="com.inadaydevelopment.myapp.MainActivity">
  107.  
  108.     <LinearLayout
  109.         android:focusable="true"
  110.         android:focusableInTouchMode="true"
  111.         android:layout_width="0px"
  112.         android:layout_height="0px"/>
  113.  
  114.     <ListView
  115.         android:id="@android:id/list"
  116.         android:layout_width="match_parent"
  117.         android:layout_height="match_parent"
  118.         android:descendantFocusability="afterDescendants"
  119.         >
  120.  
  121.     </ListView>
  122.  
  123. </android.support.constraint.ConstraintLayout>
  124.  
  125.  
  126. // ----------- R.layout.cell_textfield ---------------
  127. <?xml version="1.0" encoding="utf-8"?>
  128. <RelativeLayout
  129.     xmlns:android="http://schemas.android.com/apk/res/android"
  130.     android:layout_width="match_parent"
  131.     android:layout_height="wrap_content"
  132.     android:minHeight="?attr/listPreferredItemHeight"
  133.     android:paddingStart="?attr/listPreferredItemPaddingLeft"
  134.     android:paddingEnd="?attr/listPreferredItemPaddingRight"
  135.     >
  136.  
  137.     <TextView android:id="@+id/textview1"
  138.               android:layout_height="match_parent"
  139.               android:layout_width="80dp"
  140.               android:layout_centerVertical="true"
  141.               android:text="Title"
  142.  
  143.         />
  144.  
  145.     <EditText android:id="@+id/textview2"
  146.               android:layout_width="match_parent"
  147.               android:layout_height="wrap_content"
  148.               android:layout_toEndOf="@id/textview1"
  149.               android:layout_centerVertical="true"
  150.               android:text="Detail"
  151.               android:layout_marginStart="21dp"
  152.  
  153.         />
  154.  
  155. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement