ListView के अंदर फोकसेबल EditText बनाने का तरीका
डिफ़ॉल्ट रूप से ListView आइटम्स में EditText फोकस नहीं ले पाता। यह उदाहरण android:descendantFocusability="beforeDescendants" और listView.setItemsCanFocus(true) का उपयोग करके इस समस्या को हल करता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से प्रोजेक्ट बनाएं।
चरण 2: मेन लेआउट (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="4dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
</ListView>
</LinearLayout>
चरण 3: आइटम लेआउट (res/layout/listitem.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ItemCaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:singleLine="true" />
</LinearLayout>
चरण 4: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
listView.setItemsCanFocus(true); // महत्वपूर्ण!
myAdapter = new MyAdapter();
listView.setAdapter(myAdapter);
}
private class MyAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
ArrayList myItems = new ArrayList<>();
MyAdapter() {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 7; i++) {
myItems.add("Caption " + i);
}
notifyDataSetChanged();
}
@Override public int getCount() { return myItems.size(); }
@Override public Object getItem(int position) { return position; }
@Override public long getItemId(int position) { return position; }
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.listitem, null);
holder.caption = convertView.findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.caption.setText(myItems.get(position));
holder.caption.setTag(position); // पोजीशन स्टोर करने के लिए tag उपयोग करें
holder.caption.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
int pos = (int) v.getTag();
EditText editText = (EditText) v;
myItems.set(pos, editText.getText().toString());
}
});
return convertView;
}
}
static class ViewHolder {
EditText caption;
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
सुधार और नोट्स
- मूल कोड में
LauncherActivity.ListItemका गलत उपयोग था; यहां साधारणArrayListउपयोग किया गया है। windowSoftInputMode="adjustPan"कीबोर्ड खुलने पर लेआउट को एडजस्ट करता है।- आधुनिक ऐप्स में RecyclerView प्रेफर्ड है।