① 安卓開發怎麼通過點擊按鈕彈出以下窗口
你可以獲取對話框的點擊事件,比如點擊了確定然後你就跳轉
AlertDialog.Builder builder = new Builder(CommentActivity.this);
builder.setMessage("確定要跳轉嗎?");
builder.setTitle("提示");
builder.setPositiveButton("確認",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
這里跳轉到你想要去的頁面
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
arg0就是該listener的介面啊,通過這個參數就可以關閉對話框。
跳到想去的頁面就startIntent就好了,你把那一行中文換成 Intent it = new Intent(this,UserActivity.class); startActivity(it); 當然要跳去哪個頁面就你自己決定
AlertDialog.Builder builder = new Builder(CommentActivity.this);
builder.setMessage("確定要跳轉嗎?");
builder.setTitle("提示");
builder.setPositiveButton("確認",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
Intent it = new Intent(this,UserActivity.class);
startActivity(it);
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
② 如何實現android中監聽來電並生成懸浮窗體提示
android.permission.READ_PHONE_STATE"許可權
Xml代碼
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
還需要注冊來電監聽,目前我的處理方式是接收開機廣播,然後在接收到廣播後注冊來電監聽。接收開機廣播需要有「android.permission.RECEIVE_BOOT_COMPLETED」許可權,manifest中申明如下
java代碼
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
然後注冊廣播接收類
Xml代碼
<receiver android:name=".PhoneBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
PhoneBootReceiver中注冊監聽來電,首先得獲取系統服務「TELEPHONY_SERVICE」
Java代碼
TelephonyManager telM = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
然後添加監聽
Java代碼
telM.listen(new TelListener(context), PhoneStateListener.LISTEN_CALL_STATE);
TelListener是自己定義的電話狀態監聽類,繼承自PhoneStateListener,監聽來電只需要實現onCallStateChanged(int
state, String incomingNumber)方法。
咳咳...標題上說了彈出懸浮窗口,其實懸浮窗口就是在WindowManager中添加一個View,這個功能我也是在TelListener實現的。要想實現懸浮窗口,首先得有「android.permission.SYSTEM_ALERT_WINDOW」的許可權,在manifest中申明如下:
Xml代碼
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
WindowManager需要通過context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
來獲取。
先把TelListener源碼放出來,再詳解
Xml代碼
public class TelListener extends PhoneStateListener {
private Context context;
private WindowManager wm;
private TextView tv;
public TelListener(Context context){
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
if(state == TelephonyManager.CALL_STATE_RINGING){
wm = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
<span style="white-space: pre;"> </span>params.format = PixelFormat.RGBA_8888;
tv = new TextView(context);
tv.setText("這是懸浮窗口,來電號碼:" + incomingNumber);
wm.addView(tv, params);
}else if(state == TelephonyManager.CALL_STATE_IDLE){
if(wm != null){
wm.removeView(tv);
}
}
}
}
state
= TelephonyManager.CALL_STATE_RINGING表示有新的來電,state =
TelephonyManager.CALL_STATE_IDLE表示電話中斷(可能理解不是很准確,電話掛斷的時候state會和TelephonyManager.CALL_STATE_IDLE相等)
定義窗口布局
Java代碼
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
設置窗口類型在所有窗口之上
Java代碼
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
別忘了
Java代碼
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
如果沒有這句話的話,在生成懸浮窗口後,懸浮窗口後的界面上東西都不能點。這句話的目的是讓懸浮窗口失去焦點。
背景透明
Java代碼
params.format = PixelFormat.RGBA_8888;
本例中懸浮窗口只是顯示一個TextView其內容為「這是懸浮窗口,來電號碼:xxxxxx」,最後將TextView添加到窗體中
Java代碼
wm.addView(tv, params);
在電話中斷後將TextView移除,否則會一直顯示的...
Java代碼
wm.removeView(tv);
啦..本文就到這兒了...
「啥?要可移動的?」
要想可以拖動的話,那給TextView添加setOnTouchListener,實現OnTouchListener的onTouchListener方法。
Java代碼
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
修改為
Java代碼
params.type = WindowManager.LayoutParams.TYPE_PHONE;
因為TYPE_SYSTEM_OVERLAY的話是TextView獲取不到輸入焦點,也就沒法拖動了哈。
③ 如何在Android中打開一個窗口
1、 首先在默認工程中新建一個Activity
2、添加動作屬性
在activity_main.xml文件中添加動作動作屬性
android:onClick="OpenNewWindow"
OpenNewWindow是自己取的名字
3、添加動作函數
在MainActivity.java文件中添加:
import android.view.View;
然後在添加:
public void OpenNewWindow(View view){
//打開一個新的窗口
Intent intent = new Intent(this,MainActivity2.class);
startActivity(intent);
Toast.makeText(this, "Toast", Toast.LENGTH_SHORT).show();
}
其中:
Intent intent = new Intent(this,MainActivity2.class);
是定義一個意圖,MainActivity2.class是要打開的窗口
startActivity(intent);
是激活這個意圖
Toast.makeText(this, "Toast", Toast.LENGTH_SHORT).show();
作為調試的時候看的,可以刪除,作用是在屏幕是顯示有沒有啟動這個動作。
請注意,為了讓系統能夠將這個方法與在android:onClick屬性中提供的方法名字匹配,它們的名字必須一致,特別是,這個方法必須滿足以下條件:
公共的
沒有返回值
有一個唯一的視圖(View)參數
使用上述方法添加Activity,在調試的時候需要用Andriod4,在此說明另外一種添加Activity的方法。
雙擊Manifest.xml文件,選中Application選項卡,向下拉,找到Application Nodes,點擊添加,跳出以下對話框:
選擇Activity選項,點擊OK。
然後選中新建的Activity,點擊右邊Name,在彈出的對話框中填入名字就可以了。然後,添加xml文件,名字任意取,當然,最好是同名文件,以後好找,填寫相應文檔既可。
Xml文件:
<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=".MainActivity3" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AText1" />
</RelativeLayout>
其中RelativeLayout說明是相對布局。
④ android怎麼將彈出窗口透明
1. 在res/values 下建立color.xml
<resources>
<color name="transparent_background">#80ffffff</color>
</resources>
PS: #80是透明度的值(即80%透明),ffffff是顏色值(為黑色)
2. 在res/values下建立style.xml
<resources> <style name="Transparent" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@color/transparent_background</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item> </style></resources>
PS: parent="android:style/Theme.Dialog" 是將activity設置為彈出式窗口
3. 在AndroidManifest.xml中找到要彈出的activity,加入theme:
<activity android:name="ActivityName" android:theme="@style/Transparent" />完成上面設置後,你的activity就已經是透明的了,但是該Activity中的控制項還沒有透明,如果還需要控制項透明,則需要在該activity的代碼中加入如下代碼:
//設置activity中的控制項透明 Window window = getWindow(); WindowManager.LayoutParams wl = window.getAttributes(); wl.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; wl.alpha=0.95f;//設置透明度,0.0為完全透明,1.0為完全不透明 window.setAttributes(wl);
⑤ android中PopupWindow彈出窗體後,為什麼不能點擊其他控制項
設置popupwindow可點擊
mPopupWindow.setFocusable(true); // 設置PopupWindow可獲得焦點
mPopupWindow.setTouchable(true); // 設置PopupWindow可觸摸
補充:
默認打開popupwindow是沒有焦點和不可點擊的。因此需要設置點擊事件。
⑥ 如何讓 Android 應用鎖屏時彈出窗口
這種情況的話是需要有相關的設置的。
只有很少的軟體支持在鎖屏狀態下彈出對話框比如說QQ就可以但是微信或者是其他的好像都沒有這個功能沒有這個功能的話就沒有辦法實現了,QQ的話可以直接在QQ的設置裡面設置就可以了。
⑦ 安卓手機總是彈出廣告怎麼處理
如果您使用的是華為手機,手機第三方應用在鎖屏界面或解鎖後出現廣告推送,若您不想看到廣告界面,可嘗試以下方式關閉:
1.確認產生鎖屏廣告的應用,再去設置界面中找到對應的應用關閉通知:
打開設置,搜索進入應用管理,找到前面確認的應用,點擊 通知/通知管理 , 關閉允許通知。(關閉通知後可能會影響軟體正常消息接收,請您謹慎操作)
2.檢查廣告頁面中是否有設置按鈕,若有,點擊並選擇鎖屏顯示關閉。
3.禁止應用使用懸浮窗顯示:打開設置,搜索並進入許可權管理,點擊許可權界面打開懸浮窗,關閉不常用應用開關。
4.如果您不需要使用「產生鎖屏廣告」的應用,建議您直接卸載此應用即可。
5. 如果以上方法仍無法解決您的問題,建議您提前備份好數據(QQ、微信等第三方應用需單獨備份)後恢復出廠設置。
⑧ android開發百度地圖怎麼實現自定義彈出窗口
基本原理就是用ItemizedOverlay來添加附加物,在OnTap方法中向MapView上添加一個自定義的View(如果已存在就直接設為可見),下面具體來介紹我的實現方法:
一、自定義覆蓋物類:MyPopupOverlay,這個類是最關鍵的一個類ItemizedOverlay,用於設置Marker,並定義Marker的點擊事件,彈出窗口,至於彈出窗口的內容,則通過定義Listener,放到Activity中去構造。如果沒有特殊需求,這個類不需要做什麼改動。代碼如下,popupLinear這個對象,就是加到地圖上的自定義View:
public class MyPopupOverlay extends ItemizedOverlay<OverlayItem> {
private Context context = null;
// 這是彈出窗口, 包括內容部分還有下面那個小三角
private LinearLayout popupLinear = null;
// 這是彈出窗口的內容部分
private View popupView = null;
private MapView mapView = null;
private Projection projection = null;
// 這是彈出窗口內容部分使用的layoutId,在Activity中設置
private int layoutId = 0;
// 是否使用網路帶有A-J字樣的Marker
private boolean useDefaultMarker = false;
private int[] defaultMarkerIds = { R.drawable.icon_marka,
R.drawable.icon_markb, R.drawable.icon_markc,
R.drawable.icon_markd, R.drawable.icon_marke,
R.drawable.icon_markf, R.drawable.icon_markg,
R.drawable.icon_markh, R.drawable.icon_marki,
R.drawable.icon_markj, };
// 這個Listener用於在Marker被點擊時讓Activity填充PopupView的內容
private OnTapListener onTapListener = null;
public MyPopupOverlay(Context context, Drawable marker, MapView mMapView) {
super(marker, mMapView);
this.context = context;
this.popupLinear = new LinearLayout(context);
this.mapView = mMapView;
popupLinear.setOrientation(LinearLayout.VERTICAL);
popupLinear.setVisibility(View.GONE);
projection = mapView.getProjection();
}
@Override
public boolean onTap(GeoPoint pt, MapView mMapView) {
// 點擊窗口以外的區域時,當前窗口關閉
if (popupLinear != null && popupLinear.getVisibility() == View.VISIBLE) {
LayoutParams lp = (LayoutParams) popupLinear.getLayoutParams();
Point tapP = new Point();
projection.toPixels(pt, tapP);
Point popP = new Point();
projection.toPixels(lp.point, popP);
int xMin = popP.x - lp.width / 2 + lp.x;
int yMin = popP.y - lp.height + lp.y;
int xMax = popP.x + lp.width / 2 + lp.x;
int yMax = popP.y + lp.y;
if (tapP.x < xMin || tapP.y < yMin || tapP.x > xMax
|| tapP.y > yMax)
popupLinear.setVisibility(View.GONE);
}
return false;
}
@Override
protected boolean onTap(int i) {
// 點擊Marker時,該Marker滑動到地圖中央偏下的位置,並顯示Popup窗口
OverlayItem item = getItem(i);
if (popupView == null) {
// 如果popupView還沒有創建,則構造popupLinear
if (!createPopupView()){
return true;
}
}
if (onTapListener == null)
return true;
popupLinear.setVisibility(View.VISIBLE);
onTapListener.onTap(i, popupView);
popupLinear.measure(0, 0);
int viewWidth = popupLinear.getMeasuredWidth();
int viewHeight = popupLinear.getMeasuredHeight();
LayoutParams layoutParams = new LayoutParams(viewWidth, viewHeight,
item.getPoint(), 0, -60, LayoutParams.BOTTOM_CENTER);
layoutParams.mode = LayoutParams.MODE_MAP;
popupLinear.setLayoutParams(layoutParams);
Point p = new Point();
projection.toPixels(item.getPoint(), p);
p.y = p.y - viewHeight / 2;
GeoPoint point = projection.fromPixels(p.x, p.y);
mapView.getController().animateTo(point);
return true;
}
private boolean createPopupView() {
// TODO Auto-generated method stub
if (layoutId == 0)
return false;
popupView = LayoutInflater.from(context).inflate(layoutId, null);
popupView.setBackgroundResource(R.drawable.popupborder);
ImageView dialogStyle = new ImageView(context);
dialogStyle.setImageDrawable(context.getResources().getDrawable(
R.drawable.iw_tail));
popupLinear.addView(popupView);
android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.topMargin = -2;
lp.leftMargin = 60;
popupLinear.addView(dialogStyle, lp);
mapView.addView(popupLinear);
return true;
}
@Override
public void addItem(List<OverlayItem> items) {
// TODO Auto-generated method stub
int startIndex = getAllItem().size();
for (OverlayItem item : items){
if (startIndex >= defaultMarkerIds.length)
startIndex = defaultMarkerIds.length - 1;
if (useDefaultMarker && item.getMarker() == null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[startIndex++]));
}
}
super.addItem(items);
}
@Override
public void addItem(OverlayItem item) {
// TODO Auto-generated method stub
// 重載這兩個addItem方法,主要用於設置自己默認的Marker
int index = getAllItem().size();
if (index >= defaultMarkerIds.length)
index = defaultMarkerIds.length - 1;
if (useDefaultMarker && item.getMarker() == null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[getAllItem().size()]));
}
super.addItem(item);
}
public void setLayoutId(int layoutId) {
this.layoutId = layoutId;
}
public void setUseDefaultMarker(boolean useDefaultMarker) {
this.useDefaultMarker = useDefaultMarker;
}
public void setOnTapListener(OnTapListener onTapListener) {
this.onTapListener = onTapListener;
}
public interface OnTapListener {
public void onTap(int index, View popupView);
}
}