【问题标题】:Switch between two or more edit text in android在android中的两个或多个编辑文本之间切换
【发布时间】:2013-04-04 10:30:48
【问题描述】:

假设我的布局中有两个或更多EditTextview,并且在运行时我错误地选择了第二个视图并用文本填充它;现在我想转到以前的 EditText 视图并在触摸增益上重点在其中写一些文本。

但我无法做到这一点。我无法获得视图焦点并在单击该特定视图时将其写入。

请看下面的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/wall">

<EditText
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:textSize="30dp"
    android:focusable="true"
    android:hint="@string/Title" />

<EditText
    android:id="@+id/body"
    android:layout_width="fill_parent"
    android:layout_height="350dp"
    android:textSize="25dp"
    android:paddingTop="45dp"
    android:gravity="top"
    android:inputType="textMultiLine|textNoSuggestions"
    android:ems="10"
    android:focusable="true"
    android:hint="@string/program" />

<TextView
    android:id="@+id/CalDisplay"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
   android:textSize="20dp"
    android:layout_below="@+id/save" />

<Button
    android:id="@+id/Calculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/body"
    android:layout_toRightOf="@+id/RunProgram"
    android:background="@drawable/custom_button"
    android:textColor="#ffffff"
    android:text="@string/Calculate" />

<Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/Calculate"
    android:layout_alignBottom="@+id/Calculate"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/RunProgram"
    android:background="@drawable/custom_button"
    android:textColor="#ffffff"
    android:text="@string/Save"
     />

<Button
    android:id="@+id/RunProgram"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/body"
    android:layout_centerHorizontal="true"
    android:background="@drawable/custom_button"
    android:textColor="#ffffff"
    android:text="@string/Run" />

</RelativeLayout>   

【问题讨论】:

  • 所以基本上你在第二个编辑文本中输入文本后无法在第一个编辑文本中输入文本??
  • 为什么在两个edittext中都使用focusable=true。它只会接收一个。

标签: android android-layout android-widget android-edittext


【解决方案1】:
Try this...

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:hint="Title"
        android:textSize="15dp" />

    <EditText
        android:id="@+id/body"
        android:layout_width="fill_parent"
        android:layout_height="350dp"
        android:layout_below="@+id/title"
        android:layout_marginTop="5dp"
        android:gravity="left"
        android:hint="program"
        android:inputType="textMultiLine|textNoSuggestions"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/CalDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/save"
        android:textSize="20dp" />

    <LinearLayout
        android:id="@+id/linearButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/body" >

        <Button
            android:id="@+id/Calculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Calculate"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/RunProgram"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Run"
            android:textColor="#ffffff" />
    </LinearLayout>

    </RelativeLayout>

【讨论】:

    【解决方案2】:

    RelativeLayout 替换为LinearLayout。因为您的 EditText 似乎彼此重叠(导致焦点问题),或者至少为它们添加了一些各自的对齐方式。

    例如,将以下内容添加到您的第二个EditText

    android:layout_below="@id/title"
    

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多