我正在使用抽屉布局,里面有一个ListView和RelativeLayout,用于我的应用程序上的汉堡包菜单。单击ListView上的项目时,我想要更改为新活动。但是,当我单击ListView上的项目时,没有任何事情发生。我的代码:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="b.calvin.com.dirtymoney.MainActivity"
android:focusable="false"
android:focusableInTouchMode="false">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hourlyRate"
android:layout_toRightOf="@+id/enterHourlyRate"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Hourly Rate:"
android:id="@+id/enterHourlyRate"
android:layout_centerVertical="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make It Rain"
android:id="@+id/start"
android:layout_below="@+id/hourlyRate"
android:layout_toLeftOf="@+id/stop"
android:layout_toStartOf="@+id/stop"
android:layout_marginRight="37dp"
android:layout_marginEnd="37dp"
android:layout_marginTop="77dp"
android:visibility="invisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/stop"
android:layout_alignBottom="@+id/start"
android:layout_alignRight="@+id/hourlyRate"
android:layout_alignEnd="@+id/hourlyRate"
android:layout_marginRight="42dp"
android:layout_marginEnd="42dp"
android:layout_alignTop="@+id/start"
android:visibility="invisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="@+id/confirmHourlyRate"
android:layout_below="@+id/hourlyRate"
android:layout_alignParentRight="true"/>
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:layout_above="@+id/stop"
android:layout_alignLeft="@+id/start"
android:layout_alignStart="@+id/start" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="$$ Money"
android:id="@+id/money"
android:layout_below="@+id/start"
android:layout_toLeftOf="@+id/stop"
android:layout_toStartOf="@+id/stop" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:id="@+id/edit"
android:layout_below="@+id/hourlyRate"
android:layout_toLeftOf="@+id/confirmHourlyRate"
android:layout_toStartOf="@+id/confirmHourlyRate"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day: $"
android:id="@+id/dayText"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/edit"
android:layout_alignStart="@+id/edit" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Week: $"
android:id="@+id/weekText"
android:layout_below="@+id/dayText"
android:layout_alignLeft="@+id/dayText"
android:layout_alignStart="@+id/dayText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Month: $"
android:id="@+id/monthText"
android:layout_below="@+id/weekText"
android:layout_alignLeft="@+id/weekText"
android:layout_alignStart="@+id/weekText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year: $"
android:id="@+id/yearText"
android:layout_below="@+id/monthText"
android:layout_alignLeft="@+id/monthText"
android:layout_alignStart="@+id/monthText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total: $"
android:id="@+id/totalText"
android:layout_below="@+id/yearText"
android:layout_alignLeft="@+id/yearText"
android:layout_alignStart="@+id/yearText"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/dayMoney"
android:layout_toRightOf="@+id/dayText"
android:layout_alignTop="@+id/dayText"
android:layout_alignBottom="@+id/dayText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/weekMoney"
android:layout_toRightOf="@+id/weekText"
android:layout_alignTop="@+id/weekText"
android:layout_alignBottom="@+id/weekText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/monthMoney"
android:layout_toRightOf="@+id/monthText"
android:layout_alignTop="@+id/monthText"
android:layout_alignBottom="@+id/monthText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/yearMoney"
android:layout_toRightOf="@+id/yearText"
android:layout_alignTop="@+id/yearText"
android:layout_alignBottom="@+id/yearText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/totalMoney"
android:layout_toRightOf="@+id/totalText"
android:layout_alignTop="@+id/totalText"
android:layout_alignBottom="@+id/totalText"/>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "BroadcastTest";
private Intent intent;
private Button start;
private Button stop;
private Button confirmRate;
private Button edit;
private EditText hourlyRate;
private TextView money;
private TextView dayMoney;
private TextView weekMoney;
private TextView monthMoney;
private TextView yearMoney;
private TextView totalMoney;
private double moneyEarned;
private double secondsRate;
private double hrlyRate;
private double currentDayMoney;
private double currentWeekMoney;
private double currentMonthMoney;
private double currentYearMoney;
private double currentTotalMoney;
private String savedDayMoney;
private String savedWeekMoney;
private String savedMonthMoney;
private String savedYearMoney;
private String savedTotalMoney;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView)findViewById(R.id.navList);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
confirmRate = (Button) findViewById(R.id.confirmHourlyRate);
edit = (Button) findViewById(R.id.edit);
hourlyRate = (EditText) findViewById(R.id.hourlyRate);
money = (TextView) findViewById(R.id.money);
dayMoney = (TextView) findViewById(R.id.dayMoney);
weekMoney = (TextView) findViewById(R.id.weekMoney);
monthMoney = (TextView) findViewById(R.id.monthMoney);
yearMoney = (TextView) findViewById(R.id.yearMoney);
totalMoney = (TextView) findViewById(R.id.totalMoney);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void addDrawerItems() {
String[] osArray = { "New Shift", "Poop Slips", "Statistics", "Help"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return true;
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Menu");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
答案 0 :(得分:0)
在bigevilbrain
addDrawerItems();
答案 1 :(得分:0)
通过添加
修复mDrawerList.bringToFront();
mDrawerLayout.requestLayout();
到
public void onDrawerOpened(View drawerView)
答案 2 :(得分:0)
我在OnItemClick
方法中看不到任何开始新活动的声明。您只是获得string
并在同一活动中制作Toast
。尝试点击id
点击Item
,然后根据Activity
开始id
。
这里有一些代码供您使用。
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(currentActivity.this, parent.class);
startActivity(intent);
}
});
}
如果此代码能够让您的应用运行,请告诉我们。并且接受了这个答案。