簡體   English   中英

Android按鈕點擊問題

[英]Android Button Click issue

嗨,我在理解為什么這不起作用時遇到問題。 我有一個按鈕,當您單擊它時,它不會進入新活動。 我試圖只單擊一個簡單的按鈕,然后轉到另一個上面有一個片段的活動。

我的事件點擊代碼如下:

public class PlanMeMainFragment extends Fragment {

    private Button mNewButton, mExistingButton;

        mNewButton = (Button)v.findViewById(R.id.new_event_button);
        mNewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((PlanMeMainActivity) getActivity()).newActivityToLaunch(1);

            }
        });
        return  v;
    }
}

我的活動

public class PlanMeMainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.planme_main_fragment);
    }

    public void newActivityToLaunch(int i) {
        Intent myIntent;
        switch(i){
        case 1: myIntent = new Intent(PlanMeMainActivity.this, NewEventSetupActivity.class);
        }

        startActivity(myIntent);
    }
}

我這樣列出清單:

<activity
    android:name="com.pctoolman.planme.app.PlanMeMainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.pctoolman.planme.app.NewEventSetupActivity"
    android:label="@string/setup">
</activity>

嘗試其他方法,答案的@vdisawar給了我這個主意:

在您的片段中:

// in your onClick method  
// call a method in your Activity 
((PlanMeMainActivity) getActivity()).newActivityToLaunch(); 

在您的FragmentActivity中:

// newActivityToLaunch method  
public void newActivityToLaunch() {
    Intent myIntent = new Intent(PlanMeMainActivity.this, NewEventSetupActivity.class);
    startActivity(myIntent);
}  

有了這個技巧,您將能夠使用相同的方法從不同的片段調用不同的活動,讓我們看一個簡單的示例:

FragmentA想要啟動Activity1:

// call a method with an integer 
((MainActivity) getActivity()).newActivityToLaunch(1);  

在另一個片段(FragmentB)中,該片段要啟動Activity2:

// call the same method with another integer 
((MainActivity) getActivity()).newActivityToLaunch(2);  

然后在您的FragmentActivity中,方法是:

// newActivityToLaunch method  
public void newActivityToLaunch(int i) {
    // create a general intent
    Intent myIntent = null;
    // switch i received from fragments
    switch(i) {
        // received from FragmentA
        case 1: myIntent = new Intent(MainActivity.this, Activity1.class);
        // received from FragmentB
        case 2: myIntent = new Intent(MainActivity.this, Activity2.class);
    }
    // start the activity
    startActivity(myIntent);
}  

希望這可以幫助。

嘗試更改此:

<activity 
   android:name="com.pctoolman.planme.app.NewEventSetupActivity"
   android:label="@string/setup">
</activity>

對此:

<activity
    android:name="com.pctoolman.planme.app.NewEventSetupActivity"
    android:label="@string/setup">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

您應該在Fragment類中創建一個接口,並在擴展您的片段的活動中實現該接口。 這樣,在活動中單擊按鈕時,接口方法將在片段的onClick方法中調用,並將在活動中執行該接口方法的代碼。 在這種情況下,您的方法將啟動一個意圖。

步驟1:在您的片段類中創建接口

public interface OnFragmentInteractionListener {
    public void onFragmentInteraction();
}

步驟2:在您的片段類中為接口創建一個變量

private OnFragmentInteractionListener mListener;

步驟3:通過重寫onAttach方法並確保在片段活動中實現了偵聽器,初始化了偵聽器

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

步驟4:處理片段類中的onClick方法

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

    mNewButton = (Button)v.findViewById(R.id.new_event_button);
    mNewButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onFragmentInteraction(); // executes this method in the activity
        }
    });
    return  v;
}

步驟5:在您的活動中實現界面

public MainActivity extends Activity implements 
          PlanMeMainFragment.OnFragmentInteractionListener

覆蓋活動中的接口方法

public void onFragmentInteraction(){
    Intent intent = new Intent(this, NewEventSetupActivity.class);
    startActivity(intent);
}

步驟7:在片段類中分離偵聽器

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

有關更多信息,請參閱Android培訓課程與其他片段進行通信

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM