Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> [android] 練習PopupWindow實現對話框,androidpopupwindow

[android] 練習PopupWindow實現對話框,androidpopupwindow

編輯:關於android開發

[android] 練習PopupWindow實現對話框,androidpopupwindow


練習使用Dialog實習對話框

package com.example.tsh;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Dialog實現對話框
        Dialog dialog=new Dialog(this,R.style.Theme_Tsh_Dialog);
        dialog.setContentView(R.layout.activity_two);
        dialog.show();
        
        
    }
}

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    
    <!-- 自定義對話框主題 -->
    <style name="Theme.Tsh.Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@null</item>
    </style>
</resources>

練習使用PopupWindow實現對話框

package com.example.tsh;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Dialog實現對話框
//        Dialog dialog=new Dialog(this,R.style.Theme_Tsh_Dialog);
//        dialog.setContentView(R.layout.activity_two);
//        dialog.show();
        
        //使用PopupWindow實現對話框
        Button dropDown=(Button) findViewById(R.id.bt_dropdown);
        dropDown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                View anchor=View.inflate(MainActivity.this, R.layout.activity_two, null);
                final PopupWindow popupWindow=new PopupWindow(anchor, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
                
                //點擊空白隱藏
                popupWindow.setTouchable(true);
                popupWindow.setTouchInterceptor(new OnTouchListener() {
                    
                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        popupWindow.dismiss();
                        return false;
                    }
                });
                popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                popupWindow.showAsDropDown(arg0);
                
            }
        });
        
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="第一個界面"/>
    <Button 
        android:id="@+id/bt_dropdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示下拉"
        />
</LinearLayout>

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved