安卓小日记练习(5)点击下栏选项显示菜单

我们已经初步处理了Listview的点击事件,今天就让我们来初步处理下下面那一行按钮的点击事件吧~

话不多说,先上图。点击年份或者月份就会出现相应的菜单选项,我们选中相应的菜单选项,就可以实现更改。


我们本来的august和2015是设置的图片,但是由于要更改,我们用imageview就相当的不方便了。所以我第一步就把它们改成了textview。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent" 
        android:layout_height="380dp" >
        </ListView>

    	 <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            
    	     
    	     <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="0dp"
                />
    	     
            <ImageView 
                android:id="@+id/image_view1"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/blacklong"
                android:clickable = "true"
                 />
            <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="0dp"
                />
            
            <TextView
                android:id="@+id/text_view1"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                
                android:clickable = "true"
                />
            <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="0dp"
                />
            
            <ImageView
                android:id="@+id/imge_view3"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/divi"
                android:clickable = "true"
                />
            <View
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="0dp"
                />
            
                <TextView
                android:id="@+id/text_view2"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                
                android:clickable = "true"
                />
            <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="0dp"
                />
            
                <ImageView
                android:id="@+id/imge_view5"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/divi"
                android:clickable = "true"
                />
            <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="0dp"
                />
            
             <ImageView
                android:id="@+id/imge_view6"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="2"
                android:src="@drawable/add"
                android:clickable = "true"
                />
            <View
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="0dp"
                />
             <ImageView
                android:id="@+id/imge_view7"
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/divi"
                android:clickable = "true"
                />
             <View
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="0dp"
                />
            
            <ImageView
                android:id="@+id/image_view8"
                android:layout_marginTop="5dip"
                android:layout_height="wrap_content"
                android:layout_weight="4"
				android:layout_width="wrap_content"
                android:src="@drawable/blacklong2"
                android:clickable = "true"
                 />
            <View
                android:layout_width="0dp"
                android:layout_weight="8"
                android:layout_height="0dp"
                />
            
            </LinearLayout>
    
</LinearLayout>


然后在活动中动态的设置它们的值就可以了。我们要显示菜单,实现就是要在menu文件下定义好菜单文件。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
<item
    android:id="@+id/JANUARY"
    android:title="JANUARY"  />

<item
    android:id="@+id/FEBRUARY"
    android:title="FEBRUARY"  />

<item
    android:id="@+id/MARCH"
	android:title="MARCH"  />

<item
    android:id="@+id/APRIL"
    android:title="APRIL"  />

<item
    android:id="@+id/MAY"
    android:title="MAY"  />

<item
    android:id="@+id/JUNE"
    android:title="JUNE"  />

<item
    android:id="@+id/JULY"
    android:title="JULY"  />

<item
    android:id="@+id/AUGUST"
    android:title="AUGUST"  />

<item
    android:id="@+id/SEPTEMBER"
    android:title="SEPTEMBER"  />

<item
    android:id="@+id/OCTOBER"
    android:title="NOVEMBER"  />

<item
    android:id="@+id/NOVEMBER"
    android:title="NOVEMBER"  />

<item
    android:id="@+id/DECEMBER"
    android:title="DECEMBER"  />
"
    

</menu>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
<item
    android:id="@+id/a2004"
    android:title="2004"  />

<item
    android:id="@+id/a2005"
    android:title="2005"  />

<item
    android:id="@+id/a2006"
	android:title="2006"  />

<item
    android:id="@+id/a2007"
    android:title="2007"  />

<item
    android:id="@+id/a2008"
    android:title="2008"  />

<item
    android:id="@+id/a2009"
    android:title="2009"  />

<item
    android:id="@+id/a2010"
    android:title="2010"  />

<item
    android:id="@+id/a2011"
    android:title="2011"  />

<item
    android:id="@+id/a2012"
    android:title="2012"  />

<item
    android:id="@+id/a2013"
    android:title="2013"  />

<item
    android:id="@+id/a2014"
    android:title="2014"  />

<item
    android:id="@+id/a2015"
    android:title="2015"  />
    
</menu>

在定义好菜单项的布局文件之后,我们应该是先定义好相应图片的点击事件(直接设置imageview对象的点击事件即可),然后我们要封装一些方法,分别对两个菜单进行处理,处理的部分我们是改变存储text值的变量,并且用这个变量settext即可。

package com.example.littled;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;


public class MainActivity extends Activity {
	
	private String monthmenu;
	private String yearmenu;
	
	private ArrayList<Object> daylist = new ArrayList<Object>();
	private ArrayList<Object> pointlist = new ArrayList<Object>();
	private ArrayList<Object> data = new ArrayList<Object>();
	
	TextView tv1;
	TextView tv2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv1=(TextView)findViewById(R.id.text_view1);
        monthmenu = "August";
		tv1.setText(monthmenu);
        
		 tv2=(TextView)findViewById(R.id.text_view2);
		 yearmenu = "2015";
		 tv2.setText(yearmenu);

        tv1.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		showPopupMenu1(tv1);
        	}
        });
        
        tv2.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		showPopupMenu2(tv2);
        	}
        });
		
        initDays();
        DayApater adapter = new DayApater(MainActivity.this,data);
        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Day d = (Day)data.get(position);
                String s = d.getDetail();
                Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                intent.putExtra("extra_data", s);
                startActivity(intent);
            }
        });
    }
        
        private void initDays()
        {
        	Day day1 = new Day("SAT","1","我去超市来着...");
        	data.add(day1);
        	Day day2 = new Day("SUN","2","今天第一次和智恩吃意大利面。");
        	data.add(day2);
        	Day day3 = new Day("MON","3","路两边的树吸走了汽车尾气");
        	data.add(day3);
        	Day day4 = new Day("TUE","4","现在每天都能吃到猪排饭了,好开心啊");
        	data.add(day4);
        	Point point = new Point(R.drawable.blackpoint);
        	data.add(point);
        	data.add(point);
        	Day day5 = new Day("FRI","7","你,黑咖啡,芝士年糕,羽毛球,成功");
        	data.add(day5);
        	Day day6 = new Day("SAT","8","DGA 7 最爱");
        	data.add(day6);
        }
        private void showPopupMenu1(View view) {
        	 PopupMenu popupMenu = new PopupMenu(this, view);
        	 popupMenu.getMenuInflater().inflate(R.menu.text1, popupMenu.getMenu());
        	 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        	  @Override
        	  public boolean onMenuItemClick(MenuItem item) {
        		  monthmenu = (String) item.getTitle();
        		  tv1.setText(monthmenu);
        	  return false;
        	  }
        	 });
        	 popupMenu.show();
        }
        private void showPopupMenu2(View view) {
       	 PopupMenu popupMenu = new PopupMenu(this, view);
       	 popupMenu.getMenuInflater().inflate(R.menu.text2, popupMenu.getMenu());
       	 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
       	  @Override
       	  public boolean onMenuItemClick(MenuItem item) {
       		  yearmenu = (String) item.getTitle();
       		  tv2.setText(yearmenu);
       	  return false;
       	  }
       	 });
       	 popupMenu.show();
       }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值