2012-01-19 37 views
0

是新的Android应用程序开发,我遇到的关于Android的碎片,edittexts,重点和按钮的交互问题。Android的碎片的EditText按钮集中

我有几个选项卡一个片段活动。大多数标签都有一个片段。但在一个选项卡中,我有一个ListFragment和一个带有edittext和按钮的片段,我称之为编辑片段。当用户选择列表项目时,编辑片段的内容显示在当前选项卡中。当试图编辑文本时,更确切地说,当击中与退格键不同的键时,新的字符不会显示,但下面的第一个按钮将获得焦点。为什么?我怎样才能避免这种行为?

早些时候,我曾经为编辑片段不同的活动和编辑的工作完美。但由于其他原因,并且因为建议,我现在只有一项活动。谢谢你的帮助!

为project_details1的XML:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/projectDetails"> 
      <LinearLayout 
        android:id="@+id/projectDetailsButtons" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="2" 
        android:layout_alignParentBottom="true" 
        android:orientation="horizontal" 
        android:background="#656565" 
        android:padding="5dp"> 
        <Button android:id="@+id/projectDetailsButton_save" 
          android:text="@string/save" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="2" /> 
        <Button android:id="@+id/projectDetailsButton_cancel" 
          android:text="@string/cancel" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="2" /> 
      </LinearLayout> 
      <ScrollView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:layout_alignParentTop="true" 
        android:layout_above="@id/projectDetailsButtons"> 
      <TableLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="15dip" 
      android:background="@color/myColor" 
      android:shrinkColumns="*" 
      android:stretchColumns="*" > 
      <TableRow> 
       <TextView 
        android:layout_width="0dip" 
        android:layout_column="0" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:padding="3dip" 
        android:text="@string/projectNo" /> 
       <TextView 
        android:id="@+id/projectDetailsProjectNr" 
        android:layout_width="0dip" 
        android:layout_column="1" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:text="@string/projectNo" 
        android:padding="3dip" > 
       </TextView> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:layout_width="0dip" 
        android:layout_column="0" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:padding="3dip" 
        android:text="@string/description" /> 
       <EditText 
        android:id="@+id/projectDetailsProjectDescription" 
        android:layout_width="0dip" 
        android:layout_column="1" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:background="@android:drawable/editbox_background" 
        android:inputType="text" 
        android:padding="3dip" > 
       </EditText> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:layout_width="0dip" 
        android:layout_column="0" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:padding="3dip" 
        /> 
       <EditText 
        android:id="@+id/projectDetailsClientName" 
        android:layout_width="0dip" 
        android:layout_column="1" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:inputType="text" 
        android:background="@android:drawable/editbox_background" 
        android:padding="3dip" > 
       </EditText> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:layout_width="0dip" 
        android:layout_column="0" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:padding="3dip" 
        android:text="@string/customerNo" /> 
       <EditText 
        android:id="@+id/projectDetailsClientNr" 
        android:layout_width="0dip" 
        android:layout_column="1" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:inputType="numberSigned" 
        android:background="@android:drawable/editbox_background" 
        android:padding="3dip" > 
       </EditText> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:layout_width="0dip" 
        android:layout_column="0" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:padding="3dip" 
        android:text="@string/bookingNo" /> 
       <EditText 
        android:id="@+id/projectDetailsBookingNr" 
        android:layout_width="0dip" 
        android:layout_column="1" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:background="@android:drawable/editbox_background" 
        android:inputType="numberSigned" 
        android:padding="3dip" > 
       </EditText> 
      </TableRow> 
      <TableRow> 
       <TextView 
        android:layout_width="0dip" 
        android:layout_column="0" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:padding="3dip" 
        android:text="@string/orderNo" /> 
       <EditText 
        android:id="@+id/projectDetailsOrderNr" 
        android:layout_width="0dip" 
        android:layout_column="1" 
        android:layout_gravity="center_vertical" 
        android:layout_span="1" 
        android:layout_weight="1" 
        android:background="@android:drawable/editbox_background" 
        android:inputType="numberSigned" 
        android:padding="3dip" > 
       </EditText> 
      </TableRow> 
    </TableLayout> 
    </ScrollView> 
    </RelativeLayout> 

方法fillEditTexts

private void fillEditTexts(final View view){ 
     final Project project = this.getShownProject(); 
     final EditText projectDescription = (EditText)view.findViewById(R.id.projectDetailsProjectDescription); 

     final TextView projectNumber = (TextView)view.findViewById(R.id.projectDetailsProjectNr); 
     final EditText bookingNumber = (EditText)view.findViewById(R.id.projectDetailsBookingNr); 
     final EditText orderNumber = (EditText)view.findViewById(R.id.projectDetailsOrderNr); 
     if(project!=null){ 
      projectDescription.setText(project.getProjectDescription() == null ? " ": project.getProjectDescription()); 
      if(project.getProjectNumber()!=null){ 
       projectNumber.setText(project.getProjectNumber().toString()); 
      } 
      if(project.getBookingNumber()!=null){ 
       bookingNumber.setText(project.getBookingNumber().toString()); 
      } 
      if(project.getOrderNumber()!=null){ 
       orderNumber.setText(project.getOrderNumber().toString()); 
      } 
     } 

     final Button cancel = (Button) view.findViewById(R.id.projectDetailsButton_cancel); 
     cancel.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       ProjectDetailsFragment.this.goBack(); 
      } 
     }); 

     final Button save = (Button) view.findViewById(R.id.projectDetailsButton_save); 
     save.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       ProjectDetailsFragment.this.saveChanges(view); 
       ProjectDetailsFragment.this.goBack(); 
      } 
     }); 
    } 
+0

你正确执行'Fragment'生命周期的方法呢?更具体地说...你是否在'onCreateView()'中正确地膨胀了你的布局? –

+0

我不知道我的理解...键盘弹出但是当你试图输入的东西按钮获得焦点? –

+0

@Alex:编辑片段onCreateView()被调用,看起来像“@Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){if } 查看视图= inflater.inflate(R.layout.project_details1,container,false); this.fillEditTexts(view); 返回视图; }' – Julia

回答

1

嘛,首先...我不认为你的布局最终会显示你想要的吧显示。它看起来像你想要你的布局包含一个LinearLayout显示按钮和一个ScrollView显示一个表,是否正确?如果是这种情况,你应该使用LinearLayout(与android:orientation="vertical"一个然后确保你为了孩子的意见,你希望它们出现的方式;如果你想显示的ScrollView下面的按钮,然后复制并粘贴保存在LinearLayout按键到文件底部,应该是这个样子,

<LinearLayout> 

    <ScrollView> 
     <!-- display the grid --> 
    </ScrollView> 

    <LinearLayout> 
     <!-- display the two buttons side by side --> 
    </LinearLayout> 

</LinearLayout> 

你必须在你的ScrollView摆脱以下属性,如果你最终能切换到LinearLayout(因为它们是不被支持的类):android:layout_alignParentTop="true"android:layout_above="@id/projectDetailsButtons"

你。还应该对android:layout_weight属性保持警惕。一般来说,你想尽量避免使用嵌套权重,因为计算这些权重效率低下,并减缓了在屏幕上绘制视图的过程。例如,对于任何按钮,您都不需要layout_weight="2"属性,因为此信息是多余的 - 删除两个权重属性都将使权重默认为“0”,这应该具有相同的效果。

话虽这么说,我如果做了这些改变将解决您的问题,除非我看到了更多你的代码也不能肯定,但它肯定是朝着正确方向迈出的一步。如果我猜的问题是什么,我会说,是因为你的XML文件与嵌套布局散落并不真正显示了自己的看法,你想要的方式你Fragment无法正确充气布局onCreateView()至。尝试做一些改变,并告诉我它是如何发生的。

+0

非常感谢您的回答!用'LinearLayout'替换'RelativeLayout'解决了这个问题。 – Julia

+0

注意未来的布局权重:)。很高兴听到你启动并运行! –