20150618_Andriod _set Dialog_弹出式菜单

参考地址: http://blog.csdn.net/zhyl8157121/article/details/8169172 
                  http://blog.csdn.net/zhyl8157121/article/details/8709048

*************************************************************************
package com.example.ht;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
 
import android.app.Activity; 
import android.app.Dialog; 
//import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast;

public class F2_TC extends ActionBarActivity {

private Button btn1; 
    private Button btn2; 
    private Button btn3; 
    private ListView listView; 
    private SimpleAdapter adapter; 
    private DBUtil dbUtil;

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_f2__tc);
  
   btn1 = (Button) findViewById(R.id.btn_all); 
         btn2 = (Button) findViewById(R.id.btn_add); 
         btn3 = (Button) findViewById(R.id.btn_delete); 
         listView = (ListView) findViewById(R.id.listView); 
         dbUtil = new DBUtil(); 
          
         btn1.setOnClickListener(new OnClickListener() { 
              
             @Override 
             public void onClick(View v) { 
                 hideButton(true); 
                 setListView(); 
             } 
         }); 
  
         btn2.setOnClickListener(new OnClickListener() { 
              
             @Override 
             public void onClick(View v) { 
                 hideButton(true); 
                 setAddDialog(); 
             } 
         }); 
  
         btn3.setOnClickListener(new OnClickListener() { 
              
             @Override 
             public void onClick(View v) { 
                 hideButton(true); 
                 setDeleteDialog(); 
             } 
         }); 
 
 ////////////// 
 }
 
 
 //////////////
 
 /**
     * 设置弹出删除对话框
     */ 
    private void setDeleteDialog() { 
         
        final Dialog dialog = new Dialog(F2_TC.this); 
        //dialog.setContentView(R.layout.dialog_delete);
        dialog.setContentView(R.layout.activity_f7__delete);
        dialog.setTitle("输入想要删除的货物的编号"); 
        Window dialogWindow = dialog.getWindow(); 
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); 
        dialogWindow.setGravity(Gravity.CENTER); 
        dialogWindow.setAttributes(lp); 
 
        //final EditText cNoEditText = (EditText) dialog.findViewById(R.id.editText1); 
        final EditText cNoEditText = (EditText) dialog.findViewById(R.id.editText_del);
        Button btnConfirm = (Button) dialog.findViewById(R.id.button1); 
        Button btnCancel = (Button) dialog.findViewById(R.id.button2); 
 
        btnConfirm.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                dbUtil.deleteCargoInfo(cNoEditText.getText().toString()); 
                dialog.dismiss(); 
                hideButton(false); 
                //Toast.makeText(MainActivity.this, "成功删除数据", Toast.LENGTH_SHORT).show();
                Toast.makeText(F2_TC.this, "成功删除数据", Toast.LENGTH_SHORT).show();
            } 
        }); 
 
        btnCancel.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                dialog.dismiss(); 
                hideButton(false); 
            } 
        }); 
         
        dialog.show(); 
    } 
 
    /**
     * 设置弹出添加对话框
     */ 
    private void setAddDialog() { 
 
        //final Dialog dialog = new Dialog(MainActivity.this);
     final Dialog dialog = new Dialog(F2_TC.this);
        //dialog.setContentView(R.layout.dialog_add);
     dialog.setContentView(R.layout.activity_f6_insert);
        dialog.setTitle("输入添加的货物的信息"); 
        Window dialogWindow = dialog.getWindow(); 
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); 
        dialogWindow.setGravity(Gravity.CENTER); 
        dialogWindow.setAttributes(lp); 
 
        final EditText cNameEditText = (EditText) dialog.findViewById(R.id.editText1); 
        final EditText cNumEditText = (EditText) dialog.findViewById(R.id.editText2); 
        Button btnConfirm = (Button) dialog.findViewById(R.id.button1); 
        Button btnCancel = (Button) dialog.findViewById(R.id.button2); 
 
        btnConfirm.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                 
                dbUtil.insertCargoInfo(cNameEditText.getText().toString(), cNumEditText.getText().toString()); 
                dialog.dismiss(); 
                hideButton(false); 
                //Toast.makeText(MainActivity.this, "成功添加数据", Toast.LENGTH_SHORT).show(); 
                Toast.makeText(F2_TC.this, "成功添加数据", Toast.LENGTH_SHORT).show(); 
            } 
        }); 
 
        btnCancel.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                dialog.dismiss(); 
                hideButton(false); 
            } 
        }); 
        dialog.show(); 
    } 
 
    /**
     * 设置listView
     */ 
    private void setListView() { 
 
        listView.setVisibility(View.VISIBLE); 
 
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 
 
        list = dbUtil.getAllInfo(); 
 
        adapter = new SimpleAdapter( 
                //MainActivity.this,
          F2_TC.this,
                list,  
                //R.layout.adapter_item,
                R.layout.activity_f3__select,
                new String[] { "Cno", "Cname", "Cnum" },  
                new int[] { R.id.txt_Cno, R.id.txt_Cname, R.id.txt_Cnum }); 
 
        listView.setAdapter(adapter); 
 
    } 
 
    /**
     * 设置button的可见性
     */ 
    private void hideButton(boolean result) { 
        if (result) { 
            btn1.setVisibility(View.GONE); 
            btn2.setVisibility(View.GONE); 
            btn3.setVisibility(View.GONE); 
        } else { 
            btn1.setVisibility(View.VISIBLE); 
            btn2.setVisibility(View.VISIBLE); 
            btn3.setVisibility(View.VISIBLE); 
        } 
 
    } 
 
    /**
     * 返回按钮的重写
     */ 
    @Override 
    public void onBackPressed() 
    { 
        if (listView.getVisibility() == View.VISIBLE) { 
            listView.setVisibility(View.GONE); 
            hideButton(false); 
        }else { 
            //MainActivity.this.finish();
         F2_TC.this.finish();
        } 
    } 
 
 /////////////

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.f2__tc, menu);
  return true;
 }

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

*************************************************************************

*************************************************************************
example1:

<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="com.example.ht.F6_insert" >

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/F6_Form" />

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="54dp"
        android:orientation="horizontal" >

<Button
            android:id="@+id/button1"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:text="@string/confirm" />

<Button
            android:id="@+id/button2"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dip"
            android:text="@string/cancel" />
    </LinearLayout>

<EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="@string/add_hint1" />

<EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:ems="10"
        android:hint="@string/add_hint2"
        android:inputType="number" />

</RelativeLayout>

*************************************************************************

*************************************************************************
example2

<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="com.example.ht.F7_Delete" >

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/F7_Form" />

<EditText
        android:id="@+id/editText_del"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="66dp"
        android:ems="10"
        android:hint="@string/delete_hint" />

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="47dp"
        android:orientation="horizontal" >

<Button
            android:id="@+id/button1"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:text="@string/confirm" />

<Button
            android:id="@+id/button2"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dip"
            android:text="@string/cancel" />
    </LinearLayout>

</RelativeLayout>

*************************************************************************

时间: 2024-10-05 17:47:53

20150618_Andriod _set Dialog_弹出式菜单的相关文章

C++ Button右键弹出式菜单

Button右键弹出式菜单 关键点 用类来实现 的 实现过程 新建1个类  类名CButtonPopMenu 基类CButton 新建1个菜单资源 IDR_MENU1 // ButtonPopMenu.h : header file // Attributes public: CRect m_Rect; CMenu m_Menu; int m_MenuID; // ButtonPopMenu.cpp : implementation file void CButtonPopMenu::OnRBu

弹出式菜单(下拉菜单)实现——PopupMenu

PopupMenu代表弹出式菜单,它会在指定组件上弹出PopupMenu,默认情况下,PopupMenu会显示在该组件的下方或上方.PopupMenu可增加多个菜单项,并可为菜单项增加子菜单. 使用PopupMenu创建菜单的步骤非常简单,只要如下步骤即可. 调用new PopupMenu(Context context,View anchor)创建下拉菜单,anchor代表要激发该弹出菜单的组件. 调用MenuInflater的inflate()方法将菜单资源填充到PopupMenu中. 调用

为树形控件添加弹出式菜单,并跟踪所选择的项

[cpp] view plaincopy <span style="font-size:24px;color:#ff0000;">为树形控件添加弹出式菜单,并跟踪所选择的项</span> [cpp] view plaincopy 动态弹出式菜单 [cpp] view plaincopy //获取当前右键的位置 CPoint pt; GetCursorPos(&pt); m_tree.ScreenToClient(&pt); //判断右键的位置处是

leetcode第一刷_Set Matrix Zeroes

这个题乍一看很简单,实际上还挺有技巧的.我最开始的想法是找一个特殊值标记,遇到一个0,把他所对应的行列中非零的元素标记成这个特殊值,0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0. 问题是这个特殊值怎么确定,题目中没有把取值范围给出,我怀着侥幸的心理用了最大和最小的int,都被揪了出来..如果找一个不存在于数组中的值,这个复杂度太高了. 有没有其他更好的方法呢?当然有.这个思想很巧妙,最后的结果是把所有0所在的行列都化成0,换句话说,化成0这个事情只要标记出是哪一行以及哪一列就可以了,能

android PopupMenu弹出式菜单的使用

 PopupMenu代表弹出式菜单.他会指定组件上弹出PopupMenu,默认情况下PopupMenu会显示在该组件的下方或者上方,PopupMenu可以增加多个菜单项并且可以为菜单项添加子菜单. 首先在res/menu下编写xml文件my_menu.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:icon="@drawable/ic

C++ Primer(第五版)学习笔记_6_标准模板库_set集合容器

C++ Primer(第五版)学习笔记_6_标准模板库_set集合容器 Set集合容器实现了红黑树(Red-BlackTree)的平衡二叉检索树的数据结构,在插入元素时,它会自动调整二叉树的排序,把该元素放到适当的位置. (1)确保每个子树根节点的键值大于左子树所有节点的键值,而小于右子树所有节点的键值: (2)另外,还得确保根节点左子树的高度与右子树的高度相等.这样,二叉树的高度最小,从而检索速度最快. 平衡二叉检索树的检索使用中序遍历算法,检索效率高.默认情况下,将键值由小到大遍历. 对于s

Android中创建PopupMenu弹出式菜单

之前写过一篇创建option menu的文章:Android中创建option menu 本文主要是讲如何创建PopupMenu弹出式菜单 1.首先创建menu文件menu2.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item androi

Dialog_ _dialog系统样式讲解 及 透明背景

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_TRADITIONAL);//传统主题 AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_DARK);//使用深色背景主题 AlertDialog.Builder buil

20150618_Andriod _KSOAP2_多线程

参考地址:http://blog.csdn.net/long704480904/article/details/8636734 webService:基于SOAP协议的远程调用标准,通过webService可以将不用的操作系统平台,不同的计算机语言,不同的技术整合到一起.调用webService需要导入jar包:ksoap2-android-assembly-3.4.0-jar-with-dependencies.jar包,这个包在网上可以下载,至于导入的方法 ,右键项目,选择最后一项prope