2012-01-19 36 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

注意未來的佈局權重:)。很高興聽到你啓動並運行! –