我有这个片段用按钮膨胀活动。如何使按钮转到另一个活动或片段?假设按钮1进入地图活动而按钮2进入listfragment
编辑** 我想要的只是一个带有2个按钮的活动,其中一个按钮转到片段
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//context = getApplicationContext();
Button test= (Button)findViewById(R.id.testbtn);
test.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, MyLocation.class);//this works
startActivity(intent);
}
});
Button proximityAlert= (Button)findViewById(R.id.alertbtn);
proximityAlert.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, mfragment.class);//this doesn't work, this class is a fragment list
startActivity(intent);
}
});
}
@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;
}
}
mfragment class
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v4.app.ListFragment;
public class mfragment extends ListFragment {
String[] cities = {
"hello",
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting the adapter to the layout
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
cities));
}
public void onListItemClick(ListView parent, View v, int position, long id) {
//displays a toest of the selected item in the list
//using an array to and using the position of the selected item
Toast.makeText(getActivity(),
"You have selected " + cities[position],
Toast.LENGTH_SHORT).show();
String selectedCity = cities[position];
/*//cheks if the detailfragment is in the current activity
DetailFragment detailFragment = (DetailFragment)
getFragmentManager().findFragmentById(R.id.detailFragment);
//---if the detail fragment is not in the current activity as myself---
if (detailFragment != null && detailFragment.isInLayout()) {
//---the detail fragment is in the same activity as the master---
detailFragment.setSelectedCity(selectedCity);
//calls the method set selected city and sends a string with the selcted city
} else {
*/
//---the detail fragment is in its own activity---
//this is only needed if is in portrait mode
//starts a new activity and it pass arguments to know
//which city got selected
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("city", selectedCity);
startActivity(intent);
//}
}
}
答案 0 :(得分:0)
您需要在正确的布局中指定按钮。如果您希望在活动中有一个按钮,则需要在活动的布局xml文件中指定它。然后,您将从活动的代码中使用它。同样,您需要在片段的xml布局文件中指定要放在片段中的按钮。
您无法混合活动和片段的布局,每个布局都相互独立,并且无法直接相互访问。
答案 1 :(得分:-1)
我很抱歉这样说,但你离你想要的远很远。请查看此示例:http://developer.android.com/guide/components/fragments.html#Example