大家好,我在Android上非常新,我想用片段按钮动作......我用这种方式:
Button butt1=(Button)findViewById(R.id.btn1);
butt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int1=new Intent(MainActivity.this,newactivity.class);
startActivity(int1);
finish();
}
});
问题是我不知道在哪里放置这个动作,如果在主活动类或导入片段类中,导入片段类是一个带按钮的列表,我喜欢每个按钮移动到另一个活动。
这是ImportFragment.class代码:
public class ImportFragment extends Fragment {
ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_import,container,false);
return rootView;
}
}
这是通过横向导航抽屉导致片段的主要活动代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback {
private GoogleMap mMap;
Button enobutt=(Button)findViewById(R.id.enobtn);
SupportMapFragment sMapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sMapFragment = SupportMapFragment.newInstance();
sMapFragment.getMapAsync(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
android.app.FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentManager rFm = getSupportFragmentManager();
if(!sMapFragment.isAdded())
rFm.beginTransaction().add(R.id.map, sMapFragment).commit();
else
rFm.beginTransaction().show(sMapFragment).commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
android.app.FragmentManager fn = getFragmentManager();
android.support.v4.app.FragmentManager sFm = getSupportFragmentManager();
int id = item.getItemId();
if(sMapFragment.isAdded())
sFm.beginTransaction().hide(sMapFragment).commit();
if (id == R.id.nav_camera) {
fn.beginTransaction().replace(R.id.content_frame, new ImportFragment()).commit();
} else if (id == R.id.nav_gallery) {
if(!sMapFragment.isAdded())
sFm.beginTransaction().add(R.id.map, sMapFragment).commit();
else
sFm.beginTransaction().show(sMapFragment).commit();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;}
}
我该怎么办?
答案 0 :(得分:0)
这样做....我在您的代码中进行了更改
{{1}}
答案 1 :(得分:0)
您只需在createView
中添加onButtonClick即可并添加以下行
startActivity(new Intent(getActivity(), YourOtherActivity.class));
答案 2 :(得分:0)
将您的片段编辑为
public class ImportFragment extends Fragment {
ListView listView;
private Activity activity;
public ImportFragment(Activity activity) {
this.activity = activity;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_import,container,false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Button btn1 = (Button) view.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(activity, newactivity.class);
activity.startActivity(intent);
activity.finish();
}
});
}
}
答案 3 :(得分:0)
您提出了一个好问题,但这是一个概念问题,完全取决于您想要做什么。如果您希望每次发布该片段时片段中的按钮操作都相同,则可以将OnClickListener保留在此片段中。否则使用回调方法将其放入活动中。 (见callback doc)
另一个好的做法是使用MVP模式(Model View Presenter),将视图与逻辑分开。
答案 4 :(得分:0)
试试这可以帮到你
butt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), YourActivity.class));
}
});