You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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" and focusableInTouchMode="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

  1. The EditText starts looking like a static TextView (no cursor, no focus).
  2. When tapped, it enables focus, shows the cursor, and pops up the keyboard for direct input.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:11:37