Android中如何实现在TableLayout单元格内直接输入并显示文本?
Hey there! Welcome to Stack Overflow 😊
Great question—let's get you set up to edit TableLayout cells directly without a dialog. The key issue here is that TextView isn't designed for direct user input, so we'll swap it out for an EditText (which handles input natively) and tweak its behavior to act like a clickable "cell" that only becomes editable when tapped.
Step 1: Update Your XML Layout
Replace your existing TextView with an EditText, and add attributes to make it behave like a static cell until clicked:
<TableRow android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#002ED6" android:orientation="horizontal" android:padding="0.5dp" > <EditText android:id="@+id/Row1Column1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="1dp" android:background="#fff" android:clickable="true" android:focusable="false" android:focusableInTouchMode="false" android:cursorVisible="false" android:text="" android:gravity="center" <!-- Optional: centers text like a typical table cell --> android:layout_weight="1" /> </TableRow>
focusable="false"andfocusableInTouchMode="false"prevent the EditText from grabbing focus automatically.cursorVisible="false"hides the input cursor until the cell is clicked.
Step 2: Add Click & Focus Logic in Your Activity/Fragment
Next, we'll make the cell editable when tapped, and revert it to static when the user finishes input. Here's how to do it in Kotlin:
import android.content.Context import android.os.Bundle import android.view.inputmethod.InputMethodManager import android.widget.EditText import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.your_layout_name) // Replace with your layout ID val row1Column1 = findViewById<EditText>(R.id.Row1Column1) // Handle cell click to enable editing row1Column1.setOnClickListener { it.apply { isFocusable = true isFocusableInTouchMode = true isCursorVisible = true requestFocus() } // Pop up the soft keyboard val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(row1Column1, InputMethodManager.SHOW_IMPLICIT) } // Revert to static state when focus is lost row1Column1.setOnFocusChangeListener { v, hasFocus -> if (!hasFocus) { v.apply { isFocusable = false isFocusableInTouchMode = false isCursorVisible = false } // Hide the soft keyboard val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(v.windowToken, 0) } } } }
And here's the Java version if you're not using Kotlin yet:
import android.content.Context; import android.os.Bundle; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout_name); // Replace with your layout ID EditText row1Column1 = findViewById(R.id.Row1Column1); // Enable editing on click row1Column1.setOnClickListener(v -> { v.setFocusable(true); v.setFocusableInTouchMode(true); ((EditText) v).setCursorVisible(true); v.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); }); // Revert to static state when focus is lost row1Column1.setOnFocusChangeListener((v, hasFocus) -> { if (!hasFocus) { v.setFocusable(false); v.setFocusableInTouchMode(false); ((EditText) v).setCursorVisible(false); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } }); } }
How This Works
- The
EditTextstarts looking like a staticTextView(no cursor, no focus). - When tapped, it enables focus, shows the cursor, and pops up the keyboard for direct input.
- When the user taps outside the cell (loses focus), it reverts to the static look and hides the keyboard.
If you have multiple cells, you can reuse this logic with a loop or a helper method instead of repeating code for each one. Let me know if you need help scaling this to more rows/columns!
内容的提问来源于stack exchange,提问作者RK_97

