Android - Basic Controls (Part 2) (19)

1. Menu:Menu

1.1 Knowledge points

(1) Master the use of menus in Android;

(2) Master the use of options menu (OptionsMenu);

(3) Master the use of context menu (ContextMenu);

(4) Master the use of submenu (SubMenu);

1.2 Specific details

 

 

package com.example.menuproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MenuActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE,//菜单不分组
				Menu.FIRST+1,//菜单项的id
				1,//菜单的编号
				"添加")//菜单标题
		        .setIcon(//设置菜单图标
		        R.drawable.add);
		menu.add(Menu.NONE,Menu.FIRST+2,2,"保存").setIcon(R.drawable.save);
		menu.add(Menu.NONE,Menu.FIRST+3,3,"编辑").setIcon(R.drawable.edit);
		menu.add(Menu.NONE,Menu.FIRST+4,4,"详细").setIcon(R.drawable.detail);
		menu.add(Menu.NONE,Menu.FIRST+5,5,"删除").setIcon(R.drawable.delete);
		menu.add(Menu.NONE,Menu.FIRST+6,6,"更多").setIcon(R.drawable.more);
		return true;
	}

}

 You only need to set the menu in onCreateOptionsMenu(Menu menu)

@Override
	public boolean onPrepareOptionsMenu(Menu menu){
		Toast.makeText(this, "菜单显示前操作", 0).show();
		return true;
	}

The above is a method executed before the menu is displayed. It can be overridden from the parent class to implement some logic.

@Override
	public void onOptionsMenuClosed(Menu menu){
		Toast.makeText(this, "选项关闭", 0).show();
	}

The above is a method that is executed after the menu is closed.

@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()){
		case Menu.FIRST+1:
			Toast.makeText(this, "这是添加菜单项", 0).show();
		    break;
		case Menu.FIRST+2:
			Toast.makeText(this, "这是保存菜单项", 0).show();
		    break;
		case Menu.FIRST+3:
			Toast.makeText(this, "这是编辑菜单项", 0).show();
		    break;
		case Menu.FIRST+4:
			Toast.makeText(this, "这是详情菜单项", 0).show();
		    break;
		case Menu.FIRST+5:
			Toast.makeText(this, "这是删除菜单项", 0).show();
		    break;
		case Menu.FIRST+6:
			Toast.makeText(this, "这是更多菜单项", 0).show();
		    break;
		}
		return false;
	}

The above are all used to add menu options in the Activity.

Of course, you can also add menu options in the xml file.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/m1"
        android:orderInCategory="100"
        android:title="添加"/>
    <item
        android:id="@+id/m2"
        android:orderInCategory="101"
        android:title="保存"/>
    <item
        android:id="@+id/m3"
        android:orderInCategory="102"
        android:title="编辑"/>
    <item
        android:id="@+id/m4"
        android:orderInCategory="103"
        android:title="详情"/>
    <item
        android:id="@+id/m5"
        android:orderInCategory="104"
        android:title="删除"/>
    <item
        android:id="@+id/m6"
        android:orderInCategory="105"
        android:title="更多"/>
    

</menu>

What needs to be noted here is that the xml file loaded is not a layout file but a menu file

@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	

context menu

package com.example.menuproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuActivity extends Activity {
    String data[] ={"中国高校培训","培训课程","万策智业"};
    ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.listView = new ListView(this);
		listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,this.data));
		super.registerForContextMenu(listView);//注册上下文菜单
		super.setContentView(listView);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("信息操作");
		menu.add(Menu.NONE,Menu.FIRST+1,1,"添加联系人");
		menu.add(Menu.NONE,Menu.FIRST+2,2,"查看详情");
		menu.add(Menu.NONE,Menu.FIRST+3,3,"删除信息");
		menu.add(Menu.NONE,Menu.FIRST+4,4,"编辑");
	}
	
	
}

 The above is the implementation of the context menu component

@Override
	public boolean onContextItemSelected(MenuItem item){
		switch (item.getItemId()){
		case Menu.FIRST+1:
			Toast.makeText(this, "添加联系人", 0).show();
		    break;
		case Menu.FIRST+2:
			Toast.makeText(this, "查看详情", 0).show();
		    break;
		case Menu.FIRST+3:
			Toast.makeText(this, "删除信息", 0).show();
		    break;
		case Menu.FIRST+4:
			Toast.makeText(this, "编辑", 0).show();
		    break;
	
		}
		return false;
	}

Submenu:SubMenu

@Override
	public boolean onCreateOptionsMenu(Menu menu){
		SubMenu fileMenu = menu.addSubMenu("文件");
		SubMenu editMenu = menu.addSubMenu("编辑");
		
		fileMenu.add(Menu.NONE,Menu.FIRST+1,1,"新建");
		fileMenu.add(Menu.NONE,Menu.FIRST+2,2,"打开");
		editMenu.add(Menu.NONE,Menu.FIRST+3,3,"保存");
		editMenu.add(Menu.NONE,Menu.FIRST+4,4,"撤销");
		return true;
	}

 Submenu monitoring

@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()){
		case Menu.FIRST+1:
			Toast.makeText(this, "新建子菜单", 0).show();
		    break;
		case Menu.FIRST+2:
			Toast.makeText(this, "打开子菜单", 0).show();
		    break;
		case Menu.FIRST+3:
			Toast.makeText(this, "保存子菜单", 0).show();
		    break;
		case Menu.FIRST+4:
			Toast.makeText(this, "撤销子菜单", 0).show();
		    break;
	    }
		return false;
	}

1.3 Summary

(1) Using menus during interface development can effectively manage the tool set;

(2) There are three types of menus in Android: options menu (OptionsMenu), context menu (ContextMenu) and submenu (SubMenu).

2. Implicit drawer component: SlidingDrawer

2.1 Knowledge points

(1) Master the main functions and implementation of SlidingDrawer;

(2) Master the event handling method of the SlidingDrawer component;

2.2 Specific details

 

 

<LinearLayout 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:orientation="vertical"
    tools:context=".SlidingDrawerActivity" >

    <SlidingDrawer 
        android:id="@+id/sd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:handle="@+id/img"
        android:content="@+id/content"
        >
        <ImageView 
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_8"
            />
        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
        </LinearLayout> 
    </SlidingDrawer>
    
    
    
</LinearLayout>

Define an inline LInearLayout to be able to dynamically add assemblies in the background

package com.example.slidingdrawer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.SlidingDrawer.OnDrawerScrollListener;
import android.widget.Toast;

public class SlidingDrawerActivity extends Activity {
    String data[]={"兰州","定西","武威","张掖"};
    ListView listView = null;
    SlidingDrawer sd = null;
    ImageView img = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sliding_drawer);
		LinearLayout content = (LinearLayout) super.findViewById(R.id.content);
		listView = new ListView(this);
		listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data));
		content.addView(this.listView);
		sd = (SlidingDrawer) super.findViewById(R.id.sd);
		img = (ImageView) super.findViewById(R.id.img);
		sd.setOnDrawerOpenListener(new OnDrawerOpenListenerImpl());
		sd.setOnDrawerCloseListener(new OnDrawerCloseListenerImpl());
		sd.setOnDrawerScrollListener(new OnDrawerScrollListenerImpl());
		
	}

	private class OnDrawerOpenListenerImpl implements OnDrawerOpenListener{

		@Override
		public void onDrawerOpened() {
			img.setImageResource(R.drawable.pic_6);
		}
		
	}
	private class OnDrawerCloseListenerImpl implements OnDrawerCloseListener{

		@Override
		public void onDrawerClosed() {
			img.setImageResource(R.drawable.pic_8);
			
		}
		
	}
    private class OnDrawerScrollListenerImpl implements OnDrawerScrollListener{

		@Override
		public void onScrollEnded() {
          Toast.makeText(SlidingDrawerActivity.this, "窗口拖动结束", 0).show();
		}

		@Override
		public void onScrollStarted() {
			// TODO Auto-generated method stub
			Toast.makeText(SlidingDrawerActivity.this, "窗口拖动开始", 0).show();
		}}
}

2.3 Summary

(1) Use the SlidingDrawer component to better manage button operation sets.

3. Zoom control: ZoomControls

3.1 Knowledge points

(1) Master the use of ZoomControls components;

3.2 Specific details

Use the scaling component to scale the font size.

<LinearLayout 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:orientation="vertical"
    tools:context=".ZoomControlsActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ZoomControls控件控制文字缩放" 
        android:textSize="10dp"
        />
    
    <ZoomControls 
        android:id="@+id/zoomcontrols"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        />

</LinearLayout>

 

package com.example.zoomcontrols;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ZoomControls;

public class ZoomControlsActivity extends Activity {
    int size =10;
    TextView tv = null;
    ZoomControls zc =null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zoom_controls);
		zc = (ZoomControls) super.findViewById(R.id.zoomcontrols);
		tv = (TextView) super.findViewById(R.id.tv);
		zc.setOnZoomInClickListener(new OnZoomInCLickListenerImpl());
		zc.setOnZoomOutClickListener(new OnZoomOutCLickListenerImpl());
	}
    //放大监听
    private class OnZoomInCLickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
			ZoomControlsActivity.this.size += 2;
			ZoomControlsActivity.this.tv.setTextSize(size);
		}
    	
    	
    }
    //缩小监听
    private class OnZoomOutCLickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
			ZoomControlsActivity.this.size -= 2;
			ZoomControlsActivity.this.tv.setTextSize(size);
		}
    	
    	
    }
}

3.3 Summary

(1) The ZoomControls component allows users to control the display size according to their own needs.

4. Pop-up window: PopupWindow

4.1 Knowledge points

(1) Master the basic implementation of pop-up windows;

(2) Master the event processing operations of pop-up windows;

4.2 Specific details

Pop-up windows mean adding a special display layer to the original mobile phone as a space for pop-up windows to display.

 

Since the PopupWindow component is an interface layer that can display itself directly on the interface, a special layout manager file is needed to define the PopupWindow component.

<LinearLayout 
    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:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择您所需要的状态" />
    <RadioGroup 
        android:id="@+id/checkStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <RadioButton 
            android:id="@+id/onLine"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
            android:text="在线"
            />
        <RadioButton 
            android:id="@+id/offLine"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
            android:text="离线"
            />
        <RadioButton 
            android:id="@+id/steach"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
            android:text="隐身"
            />
    </RadioGroup>
	<Button 
	    android:id="@+id/cancel"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消"
	    />
</LinearLayout>

The above layout manager is a display as PopupWindow.

Now let's define our main layout manager.

<LinearLayout 
    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:orientation="vertical" >
    <TextView
        android:id="@+id/statusInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户当前状态:在线" />
    <Button 
        android:id="@+id/popbut"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择状态"
        />

</LinearLayout>

The above is the main layout manager. Now there is another problem. Now that the PopupWindow is popped up through button events, a converter is definitely needed to set the layout manager that needs to be displayed to the PopupWindow. At this time, you need to use LayoutInflater for conversion. Now let's write the Activity program.

package com.example.popupwindowproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class PopupWindowActivity extends Activity {
	private  Button statuBut = null;
	private TextView statuInfo = null;
	private RadioGroup checkStatu = null;
	private Button cancelBut = null;
	private PopupWindow popWin = null;
	private View popView = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_popup_window);
		this.statuBut = (Button) super.findViewById(R.id.popbut);
		this.statuInfo = (TextView) super.findViewById(R.id.statusInfo);
		this.statuBut.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				LayoutInflater inflater = LayoutInflater.from(PopupWindowActivity.this);
				PopupWindowActivity.this.popView = (View) inflater.inflate(R.layout.popup_window, null);//将布局文件转换成组件
				PopupWindowActivity.this.popWin = new PopupWindow(PopupWindowActivity.this.popView,300,220);//创建一个新的PopupWindow
				PopupWindowActivity.this.popWin.setContentView(PopupWindowActivity.this.popView);//设置显示组件
				PopupWindowActivity.this.checkStatu = (RadioGroup) PopupWindowActivity.this.popView.findViewById(R.id.checkStatus);
				PopupWindowActivity.this.cancelBut = (Button) PopupWindowActivity.this.popView.findViewById(R.id.cancel);
				PopupWindowActivity.this.checkStatu.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						RadioButton rbut = (RadioButton) PopupWindowActivity.this.popView.findViewById(group.getCheckedRadioButtonId());//取得选中的组件ID
						PopupWindowActivity.this.statuInfo.setText("您选中的状态是:"+rbut.getText());//修改我们文本显示组件的内容
					}
				});
				PopupWindowActivity.this.cancelBut.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						PopupWindowActivity.this.popWin.dismiss();//隐藏PopupWindow
					}
				});
				PopupWindowActivity.this.popWin.showAtLocation(PopupWindowActivity.this.statuBut,Gravity.CENTER, 0, 0);//显示弹出窗口
			}
		});
	}
}

If it is a component, it cannot be separated from the layout file. There is a premise: there will be more content displayed in the component.

4.3 Summary

(1) Pop-up windows require a separate layout manager for configuration;

(2) You can use LayoutInflater to read component information through the configuration file;

Guess you like

Origin blog.csdn.net/weixin_41830242/article/details/132547788