Android语法错误,插入“}”以完成MethodBody

时间:2014-03-05 06:42:23

标签: android

我已经创建了一个使用OnDateListner方法选择日期的程序。我已经完成了程序,但它显示了语法错误。我通过放置按钮On按钮点击了Xmlfile点击出现了DatePickerDialog,您可以从中选择日期。进行选择并单击完成按钮后,将显示包含所选日期的Toast。 正在附上相应的代码。

当我尝试关闭OnClickListner方法时会显示错误

public class MainActivity extends Activity {

    //Declaring a button and the DatePickerDialog
    private Button btnDatePicker;
    DatePickerDialog _date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnDatePicker = (Button) findViewById(R.id.button1);

        //Setting an OnclickListener on the Button
        btnDatePicker.setOnClickListener(new OnClickListner(){

        public void onClick(View v)
        {
            //Creating an object of DatePickerDialog incontext of the Mainactivity
            //dateCallback is called which defined below
            _date=new DatePickerDialog(MainActivity.this, dateCallback, 2012, 10, 12);

    //Showing the DatePickerDialog
        _date.show();
        }
        });

        //Setting OnDateSetListener on the DatePickerDialog

        private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener() 
        {
            public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
                Toast.makeText(MainActivity.this, "The date is : " + dayOfMonth+"/"+  ++monthOfYear +"/"+  year, Toast.LENGTH_LONG).show();
            }
        };

2 个答案:

答案 0 :(得分:2)

以下是您的完整课程。你没有关闭创建活动方法。每个方法必须以起始大括号{开始,并且必须以右大括号}关闭。

import junit.framework.Test;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

    // Declaring a button and the DatePickerDialog
    private Button btnDatePicker;
    DatePickerDialog _date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnDatePicker = (Button) findViewById(R.id.button1);

        // Setting an OnclickListener on the Button
        btnDatePicker.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // Creating an object of DatePickerDialog incontext of the
                // Mainactivity
                // dateCallback is called which defined below
                _date = new DatePickerDialog(Test.this, dateCallback, 2012, 10,
                        12);

                // Showing the DatePickerDialog
                _date.show();
            }
        });

    }

    // Setting OnDateSetListener on the DatePickerDialog

    private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Toast.makeText(
                    MainActivity.this,
                    "The date is : " + dayOfMonth + "/" + ++monthOfYear + "/"
                            + year, Toast.LENGTH_LONG).show();
        }
    };
}

答案 1 :(得分:-2)

public class MainActivity extends Activity {

    //Declaring a button and the DatePickerDialog
    private Button btnDatePicker;
    DatePickerDialog _date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnDatePicker = (Button) findViewById(R.id.button1);

        //Setting an OnclickListener on the Button
        btnDatePicker.setOnClickListener(new OnClickListner(){

        public void onClick(View v)
        {
            //Creating an object of DatePickerDialog incontext of the Mainactivity
            //dateCallback is called which defined below
            _date=new DatePickerDialog(MainActivity.this, dateCallback, 2012, 10, 12);

    //Showing the DatePickerDialog
        _date.show();
        }
        });
}
        //Setting OnDateSetListener on the DatePickerDialog

        private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener() 
        {
            public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
                Toast.makeText(MainActivity.this, "The date is : " + dayOfMonth+"/"+  ++monthOfYear +"/"+  year, Toast.LENGTH_LONG).show();
            }
        };