我的导航抽屉里有一个相对布局,问题出在什么时候 我点击导航抽屉里的一个按钮,我无法吐司。所以我认为我的按钮没有响应。
我的布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- main screen -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<RelativeLayout
android:id="@+id/whatYouWantInLeftDrawer"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/black" >
<Button
android:id="@+id/login"
android:layout_below="@id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dip"
android:background="#303592"
android:text="Click Me"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
这是我的桌面活动的完整代码:
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Fragment;
import android.app.FragmentManager;
public class DeskboardActivity extends ActionBarActivity {
RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;
Button first;
String fragment_title;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deskboard);
mTitle = mDrawerTitle = getTitle();
leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
first = (Button)findViewById(R.id.login);
drawerLayout.setClickable(true);
first.setClickable(true);
first.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//displayView(0);
Toast.makeText(DeskboardActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new UpdatesFragment();
fragment_title = "Updates-BrahmShakti";
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
setTitle(fragment_title);
drawerLayout.closeDrawer(leftRL);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
public void onOpenLeftDrawer(View view)
{
drawerLayout.openDrawer(leftRL);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.deskboard, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
Toast.makeText(getApplicationContext(), "Toast working", Toast.LENGTH_SHORT).show();
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
答案 0 :(得分:1)
<强> EDITED 强>
不应该是
first.setOnClickListener(new View.OnClickListener() { // View.OnClickListener.
@Override
public void onClick(View v) {
Toast.makeText(activity.this, "Clicked", Toast.LENGTH_SHORT).show();
// activityName.this insteade of getApplicationContext.
}
});
答案 1 :(得分:0)
<Button
android:id="@+id/login"
android:layout_below="@id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dip"
android:background="#303592"
android:text="Click Me"
android:textColor="#ffffff"
android:textStyle="bold"
android:onClick="runToast"
/>
first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runToast(v)
}
});
public void runToast(View v) {
Toast.makeText(activity.this, "Clicked", Toast.LENGTH_SHORT).show();
}