2015-07-20 96 views
-1
不火

我已看過一些SO職位有相同的問題,因爲我做的:Android的碎片:單擊事件片段

  • 當我添加一個OnClickListener的視圖(在這種情況下,一個EditText)內Fragment類,click事件永遠不會被觸發。
  • 如果我將代碼從Fragment類移開並將其移入Activity中,則會正確觸發click事件。

其他SO問題提出同樣的問題,但他們的解決方案(如果有的話)並不適用於我所能告訴的情況。因此,我問這個問題 「再次」=)

首先,fragment_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="se.snapcode.lanstrafiken.MainActivityFragment"> 

    <LinearLayout 
     android:focusable="true" android:focusableInTouchMode="true" 
     android:layout_width="0px" android:layout_height="0px"/> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPostalAddress" 
     android:ems="15" 
     android:id="@+id/editTextFrom" 
     android:hint="From" 
     android:layout_alignParentEnd="true" 
     android:focusableInTouchMode="false" 
     android:clickable="true" 
     android:layout_marginBottom="20sp" 
     /> 
</LinearLayout> 

而現在的MainActivityFragment.java:

public class MainActivityFragment extends Fragment { 

    public MainActivityFragment() { } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View myView = inflater.inflate(R.layout.fragment_main, container, false); 

     EditText ed1 = (EditText) myView.findViewById(R.id.editTextFrom); 
     ed1.setText("This is a test"); 
     ed1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("Snapcode", "Test 1 2 3"); 
       Toast.makeText(v.getContext(), "test", Toast.LENGTH_LONG).show(); 
      } 
     }); 

     EditText ed2 = (EditText) myView.findViewById(R.id.editTextTo); 
     ed1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     return myView; 
    } 
} 

當我點擊的EditText, Log.d(...)根本不顯示在logcat中,並且Toast不顯示。

現在,如果我刪除有做的EditText上從片段,並將其放置在活動,而不是一切,它的工作原理:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1); 
     setSupportActionBar(toolbar); 

     EditText ed1 = (EditText) findViewById(R.id.editTextFrom); 
     ed1.setText("Test again"); 
     ed1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("Snapcode", "Test from Activity"); 
       Toast.makeText(MainActivity.this, "test", Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 

    // ... 
} 

任何想法?


旁註: 我認爲它非常糟糕設計的Android代碼的心不是「封裝」。正如你上面看到的,我可以從Fragment外部訪問Fragment內部的控件,就好像這些控件都在相同的範圍內。 R.id.editTextFrom在任何地方都可用,這讓我感到不安。它應該只在片段內部可用,除非您明確地將其設置爲「公開」。與.NET中的UserControls相比 - UserControls是真正獨立的。 我沒有嘗試過,但如果兩個單獨的片段具有相同的控制ID,會發生什麼?聞起來像網絡目前的工作(以及WebComponents試圖解決什麼)。

+0

的onClick在EditText上或TextView中不使用。你想用onClick做什麼?如果它壞了,你會使用onTouch。 –

+0

@Ted我無法重現該問題,使用Fragment的代碼和EditText在Fragment的佈局xml中獲取Toast。 –

+0

它爲我工作,如果我把代碼放在onActivitycreated方法 – has19

回答

0

我發現了這個問題。一個非常簡單的一個,完全是我的錯:

enter image description here

所以,是的......我的壞,沒有在這裏看到,沿着=向右移動)