导航抽屉没有打开webview按钮单击

时间:2015-08-22 08:19:48

标签: android android-webview navigation-drawer

我正在尝试在用户点击webview(HTML)按钮时打开导航抽屉。我有一个代码,当我使用Android按钮时工作正常但是当我使用HTML按钮时它不起作用。从左侧拖动导航抽屉时会显示导航抽屉,但在单击HTML按钮时则不会显示。

activity_main

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"
        android:orientation="vertical" >

       <!-- <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:onClick="open"
            android:text="Button" />  -->
            
            
            <WebView
        	android:id="@+id/webView1"
        	android:layout_width="match_parent"
        	android:layout_height="match_parent" />
      
    </LinearLayout>

    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/background_light"
        android:orientation="vertical"
        android:padding="5dp" >

        <fragment
            android:id="@+id/fragment1"
            android:name="com.example.webviewnav.MyListFragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
MyListFragment1

public class MyListFragment1 extends ListFragment {

	String[] month = { "January", "February", "March", "April", "May", "June",
			"July", "August", "September", "October", "November", "December" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, month);
		setListAdapter(myListAdapter);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.listfragment1, container, false);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Toast.makeText(getActivity(),
				getListView().getItemAtPosition(position).toString(),
				Toast.LENGTH_LONG).show();
	}
}
MainActivity

public class MainActivity extends Activity {
	
	private DrawerLayout drawerLayout;
	private View drawerView;
	WebView wv;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
		drawerView = (View)findViewById(R.id.drawer);
		
		/*Button buttonOpenDrawer = (Button)findViewById(R.id.opendrawer);
		buttonOpenDrawer.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				drawerLayout.openDrawer(drawerView);
			}
		}); */
		
		 wv = (WebView)findViewById(R.id.webView1);
		  wv.getSettings().setJavaScriptEnabled(true);
		  wv.addJavascriptInterface(new WebAppInterface(this), "Luke");
		  wv.loadUrl("file:///android_asset/www/mind.html"); 
		
		drawerLayout.setDrawerListener(myDrawerListener);
	} //onCreate ends

	DrawerListener myDrawerListener = new DrawerListener(){

		@Override
		public void onDrawerClosed(View drawerView) {
			//textPrompt.setText("onDrawerClosed");
		}

		@Override
		public void onDrawerOpened(View drawerView) {
			//textPrompt.setText("onDrawerOpened");
		}

		@Override
		public void onDrawerSlide(View drawerView, float slideOffset) {
			//textPrompt.setText("onDrawerSlide: " + String.format("%.2f", slideOffset));
		}

		@Override
		public void onDrawerStateChanged(int newState) {
			String state;
			switch(newState){
			case DrawerLayout.STATE_IDLE:
				state = "STATE_IDLE";
				break;
			case DrawerLayout.STATE_DRAGGING:
				state = "STATE_DRAGGING";
				break;
			case DrawerLayout.STATE_SETTLING:
				state = "STATE_SETTLING";
				break;
			default:
				state = "unknown!";
			}
				
			//textPrompt2.setText(state);
		}};  
	
		
		public class WebAppInterface {
		    Context mContext;

		    
		    WebAppInterface(Context c) {
		        mContext = c;
		    }

		    
		    @JavascriptInterface
		    public void showDialog() {
		        
		    	
		    	drawerLayout.openDrawer(drawerView);
		    	
		    	//Toast.makeText(getApplicationContext(), "Good", Toast.LENGTH_SHORT).show();
		    }
		} 
		
		/*public void open(View v){
			drawerLayout.openDrawer(drawerView);
		} */
	
	
	
	
	
	
} //Activity ends

1 个答案:

答案 0 :(得分:0)

是否会调用$v=isset($_POST['gender'])?$_POST['gender']:''; 方法?如果是这样,请尝试从UI线程调用drawerLayout.openDrawer:showDialog