我在Fragment中遇到了onClick事件的一些问题。 我在Actionbar中进行Tab-Navigation,并使用TabListener更改片段。 这很好用。
我的问题是''正常''OnClickListener(见下文)不起作用。 我尝试了很多不同的事情。这是目前的状态:
package com.artifact.actionbarfragments;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class ActionBarFragmentsActivity extends Activity {
/** Called when the activity is first created. */
public OnClickListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// lädt die ActionBar
ActionBar actionbar = getActionBar();
// ActionBar.LayoutParams(0);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayShowCustomEnabled(false);
// actionbar.setCustomView(R.layout.actionbarview);
// lädt die Tabs in die Actionbar und setzt ''eigene'' Icons
ActionBar.Tab TabA = actionbar.newTab().setIcon(R.drawable.ic_action_view_as_list);
ActionBar.Tab TabB = actionbar.newTab().setIcon(R.drawable.ic_action_directions);
ActionBar.Tab TabC = actionbar.newTab().setIcon(R.drawable.ic_action_group);
ActionBar.Tab TabD = actionbar.newTab().setIcon(R.drawable.ic_action_person);
// erstellt neue Fragmente
Fragment fragmentA = new fragmentA();
Fragment fragmentB = new fragmentB();
Fragment fragmentC = new fragmentC();
Fragment fragmentD = new fragmentD();
// Listener werden angelegt
TabA.setTabListener(new MyTabsListener(fragmentA));
TabB.setTabListener(new MyTabsListener(fragmentB));
TabC.setTabListener(new MyTabsListener(fragmentC));
TabD.setTabListener(new MyTabsListener(fragmentD));
// Tabs werden in die actionbar geladen
actionbar.addTab(TabA);
actionbar.addTab(TabB);
actionbar.addTab(TabC);
actionbar.addTab(TabD);
}
}
class NotfallButtonListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.id.fragment_container);
final Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
notfallbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notfallbtn.setText("Notfall ausgelöst");
System.out.println("Notfall Botton gedrückt");
}
});
}
}
当上述过程不起作用时,我尝试在片段中保留onClick类:
public class fragmentA extends Fragment
implements View.OnClickListener
{
Button notfallbtn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// das Layout fuer dieses Fragment laden
return inflater.inflate(R.layout.fragmenta, container, false);
// Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
// notfallbtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
class NotfallButtonListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.id.fragment_container);
final Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
notfallbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notfallbtn.setText("Notfall ausgelöst");
System.out.println("Notfall Botton gedrückt");
}
});
}
}
}
}
任何帮助将不胜感激!非常感谢!
答案 0 :(得分:3)
如果按钮位于fragmneta.xml
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// das Layout fuer dieses Fragment laden
View view= inflater.inflate(R.layout.fragmenta, container, false);
// inflate layout
Button notfallbtn = (Button) view.findViewById(R.id.notfallbtn);
// initialize button using the inflated view object
notfallbtn.setOnClickListener(this);
// listener for button
return view; // return inflated view
}
然后你已经
了public class fragmentA extends Fragment
implements View.OnClickListener
所以
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.notfallbtn:
// do something
break;
}
}
删除所有这些
@Override
public void onClick(View view) {
class NotfallButtonListener extends Activity {