Android開發(fā)實現AlertDialog中View的控件設置監(jiān)聽功能分析
本文實例講述了Android開發(fā)實現AlertDialog中View的控件設置監(jiān)聽功能。分享給大家供大家參考,具體如下:
之前給彈出的AlertDialog中的控件設置監(jiān)聽時,老是報空指針異常,之所以報空指針異常,是因為我findViewById寫的有問題,因為我們需要給彈出框中的控件設置監(jiān)聽,直接用findViewById是找不到彈出框中的控件的,需要利用Dialog.findViewById或者利用你找到的彈出框中的View,然后view.findViewById;具體看下面代碼
package com.example.mydialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
/**
* @author 鄭明亮
* @date 2015-11-4 下午1:57:31
* @version 1.0
*/
public class secondActivity extends Activity implements OnClickListener {
Button btshow,bt_emial,bt_blog;
ImageButton btcancel;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
final AlertDialog dialog=new AlertDialog.Builder(secondActivity.this).create();
dialog.show();
dialog.getWindow().setContentView(R.layout.myxml);//重點看這獲取彈出框內的視圖view
// btshow=(Button) findViewById(R.id.bt_show);
btcancel = (ImageButton) dialog.findViewById(R.id.bt_cancel);//重點看這行的Dialog
bt_blog=(Button) dialog.findViewById(R.id.bt_blog);
bt_emial=(Button) dialog.findViewById(R.id.bt_email);
bt_blog.setOnClickListener(this);
bt_emial.setOnClickListener(this);
btcancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
Toast.makeText(secondActivity.this, "clicked", 0).show();
Log.e("log", "click");
}
});
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.bt_blog:
Uri uri = Uri.parse("http://www.dhdzp.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
break;
case R.id.bt_email:
Uri uri1 = Uri.parse("http://www.dhdzp.com");
Intent it1 = new Intent(Intent.ACTION_VIEW, uri1);
startActivity(it1);
break;
default:
break;
}
}
}
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android 使用FragmentTabHost實現底部菜單功能
這篇文章主要介紹了Android 使用FragmentTabHost實現底部菜單功能,詳細給大家介紹了FragmentTabHost配合Fragment的使用方法,需要的朋友可以參考下2017-12-12
Android ApiDemo示例工程的創(chuàng)建
本文主要介紹Android ApiDemo示例工程的創(chuàng)建,這里SDK中的示例工程做了大致介紹,并說明如何創(chuàng)建ApiDemo 示例工程,有需要看自帶代碼的朋友可以參考下2016-09-09
Android studio中生成引用.aar和.jar的方法詳解
這篇文章主要是講解.aar的生成與引用,文中的內容屬于完全基礎性概念,對剛學習使用Android studio的朋友們很有幫助,有需要的可以參考學習,下面來一起看看吧。2016-09-09

