安卓开发问题:button 的click 事件 和dialog 的click事件 怎么才能共存于MainActivity的这个类里面。
MainActivity里面使用了一个Button和一个Dialog,现在要为他们添加事件方法,问题就出在分别为Button和Dialog引入OnClickListene...
MainActivity 里面使用了一个Button 和一个Dialog ,现在要为他们添加事件方法,问题就出在分别为Button和Dialog 引入OnClickListener 的这两个包上面,引入的包只有一个能起作用,就是哪个排在最前就哪个包起作用,所button和dialog 添加的事件至少有一个会报错。请问有经验的大神这个该怎么解决啊?
代码如下:
package com.example.dialogdemo;
import android.view.View.OnClickListener;
import android.content.DialogInterface.OnClickListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
EditText edit;
Button sendBu;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edittext);
sendBu=(Button)findViewById(R.id.button1);
sendBu.setOnClickListener(new OnClickListener(){
{
dialog();
}
});
}
public void dialog()
{
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("确认发送!");
builder.setMessage("是否发送这条消息?");
builder.setPositiveButton("确认发送", new OnClickListener(){
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
builder.setNegativeButton("取消发送", new OnClickListener(){
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
}
} 展开
代码如下:
package com.example.dialogdemo;
import android.view.View.OnClickListener;
import android.content.DialogInterface.OnClickListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
EditText edit;
Button sendBu;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edittext);
sendBu=(Button)findViewById(R.id.button1);
sendBu.setOnClickListener(new OnClickListener(){
{
dialog();
}
});
}
public void dialog()
{
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("确认发送!");
builder.setMessage("是否发送这条消息?");
builder.setPositiveButton("确认发送", new OnClickListener(){
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
builder.setNegativeButton("取消发送", new OnClickListener(){
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
}
} 展开
展开全部
用得着这么麻烦
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
这样就行了,dialog.dissmiss()也不用调,注意是 DialogInterface.OnClickListener
展开全部
package com.hw.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_1 = (Button) findViewById(R.id.bt_1);
Button bt_2 = (Button) findViewById(R.id.bt_2);
Button bt_3 = (Button) findViewById(R.id.bt_3);
Button bt_4 = (Button) findViewById(R.id.bt_4);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_1:
bt1();
break;
case R.id.bt_2:
bt2();
break;
case R.id.bt_3:
bt3();
break;
case R.id.bt_4:
bt4();
break;
default :
break;
}
}
private void bt4() {
// TODO Auto-generated method stub
}
private void bt3() {
// TODO Auto-generated method stub
}
private void bt2() {
// TODO Auto-generated method stub
}
private void bt1() {
//构建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("警告!");
builder.setMessage("世界上最遥远的距离是没有网络!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了确定按钮");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了取消按钮");
}
});
//最后一步 一定要记得show出来
builder.show();
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_1 = (Button) findViewById(R.id.bt_1);
Button bt_2 = (Button) findViewById(R.id.bt_2);
Button bt_3 = (Button) findViewById(R.id.bt_3);
Button bt_4 = (Button) findViewById(R.id.bt_4);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_1:
bt1();
break;
case R.id.bt_2:
bt2();
break;
case R.id.bt_3:
bt3();
break;
case R.id.bt_4:
bt4();
break;
default :
break;
}
}
private void bt4() {
// TODO Auto-generated method stub
}
private void bt3() {
// TODO Auto-generated method stub
}
private void bt2() {
// TODO Auto-generated method stub
}
private void bt1() {
//构建AlertDialog
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("警告!");
builder.setMessage("世界上最遥远的距离是没有网络!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了确定按钮");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了取消按钮");
}
});
//最后一步 一定要记得show出来
builder.show();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询