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>

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


 

posted @ 2015-06-18 09:28  追逐苦痛  阅读(211)  评论(0编辑  收藏  举报