简体   繁体   中英

catch a click event on a button in dialog

I'm at the beginning to learn Android Programming. I'm programming my first application. this have a button( Add a new friend ), and i catch a click event. this mean when click this button, application will show a dialog. in dialog also have a button(Add). i want to catch a click event when i click this button. i use View.OnClickListener() to catch this. but the application unfortunately when i click the button Add a new friend . here is my code: MainActivity.java :

package com.example.helloworld;
import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {

    private Button addNewStudent;
    public  static ArrayList<Student> listStudent;

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

        listStudent= new ArrayList<Student>();

        addNewStudent= (Button) (findViewById(R.id.btAdd));
        addNewStudent.setOnClickListener(new  OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Toast.makeText(getApplicationContext(), "This is a toast", Toast.LENGTH_SHORT).show();
//              Student sv= new Student("Nguyen Van A", 19);
//              listStudent.add(sv);

                MyDialog dialog= new MyDialog(MainActivity.this);
                dialog.show();

//              Toast.makeText(getApplicationContext(), sv.getName()+" "+ sv.getAge(), Toast.LENGTH_SHORT).show();

            }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MyDialog.java :

package com.example.helloworld;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MyDialog extends Dialog{

    Button buttonAddStudent;
    Button buttonCancel;
    EditText etName;
    EditText etAge;

    public MyDialog(Context context){
        super(context);

        buttonAddStudent= (Button) findViewById(R.id.buttonAdd);    
        buttonCancel= (Button) findViewById(R.id.buttonCancel);
        etName= (EditText) findViewById(R.id.editTextName);
        etAge= (EditText) findViewById(R.id.editTextAge);

        buttonAddStudent.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String name= etName.getText().toString();
                int age= Integer.valueOf(etAge.getText().toString());

                Student sv= new Student(name, age);
                MainActivity.listStudent.add(sv);

                //Toast.makeText(getContext(), MainActivity.listStudent.toString(), Toast.LENGTH_SHORT).show();

            }
        });

        setTitle("Add new student");
        setContentView(R.layout.dialog_layout);
    }
}

thank you for your thought on this :)

在MyDialog类中,可以使用DialogInterface.OnClickListener():

 buttonAddStudent.setOnClickListener(new DialogInterface.OnClickListener() ...

You should set content of a dialog before you call findViewById
Try this:

public MyDialog(Context context){
    super(context);

    setContentView(R.layout.dialog_layout);
    setTitle("Add new student");

    buttonAddStudent= (Button) findViewById(R.id.buttonAdd);    
    buttonCancel= (Button) findViewById(R.id.buttonCancel);
    etName= (EditText) findViewById(R.id.editTextName);
    etAge= (EditText) findViewById(R.id.editTextAge);

    buttonAddStudent.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String name= etName.getText().toString();
            int age= Integer.valueOf(etAge.getText().toString());

            Student sv= new Student(name, age);
            MainActivity.listStudent.add(sv);

            //Toast.makeText(getContext(), MainActivity.listStudent.toString(), Toast.LENGTH_SHORT).show();

        }
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM