android小程序,按钮不好使
求助android小程序,按钮不好使
//按下按钮显示时间,但我按下没有反应
package net.blogjava.mobile;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import yxs.pkg.firstAndroid.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示
// ,但是并非继承于View类,而是直接从java.lang.Object开始构造出的
void showDialog(String title,String msg)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton("确定", null);
builder.create().show();
}
public void onCreat(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得两个按钮的实例
Button btnShowDate = (Button)findViewById(R.id.btnShowDate);
Button btnShowTime = (Button)findViewById(R.id.btnShowTime);
btnShowDate.setOnClickListener(this);
btnShowTime.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnShowDate:
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
showDialog("当前日期",sdf.format(new Date()));
break;
}
case R.id.btnShowTime:
{
SimpleDateFormat sdf = new SimpleDateFormat("HH::mm::ss");
showDialog("当前日期",sdf.format(new Date()));
break;
}
}
}
}
------解决方案--------------------
可以啊,我运行了啊,点击有反应的啊,而且一切正常,只是我不明白你switch里的写法,呵呵。case语句里为什么加“{}”呢?
------解决方案--------------------
调试一下。。。
//按下按钮显示时间,但我按下没有反应
package net.blogjava.mobile;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import yxs.pkg.firstAndroid.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示
// ,但是并非继承于View类,而是直接从java.lang.Object开始构造出的
void showDialog(String title,String msg)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton("确定", null);
builder.create().show();
}
public void onCreat(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得两个按钮的实例
Button btnShowDate = (Button)findViewById(R.id.btnShowDate);
Button btnShowTime = (Button)findViewById(R.id.btnShowTime);
btnShowDate.setOnClickListener(this);
btnShowTime.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnShowDate:
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
showDialog("当前日期",sdf.format(new Date()));
break;
}
case R.id.btnShowTime:
{
SimpleDateFormat sdf = new SimpleDateFormat("HH::mm::ss");
showDialog("当前日期",sdf.format(new Date()));
break;
}
}
}
}
------解决方案--------------------
可以啊,我运行了啊,点击有反应的啊,而且一切正常,只是我不明白你switch里的写法,呵呵。case语句里为什么加“{}”呢?
------解决方案--------------------
调试一下。。。