我刚开始学习Android编程。 我正在编写我的第一个应用程序。这有一个按钮(添加新朋友),我抓住了一个点击事件。这意味着当单击此按钮时,应用程序将显示一个对话框。在对话框中也有一个按钮(添加)。我想在点击此按钮时捕获点击事件。 我使用View.OnClickListener()来捕获它。但不幸的是,当我点击按钮添加新朋友时,该应用程序。 这是我的代码: 的 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);
}
}
感谢您对此的想法:)
答案 0 :(得分:0)
在MyDialog类中,您可以使用DialogInterface.OnClickListener():
buttonAddStudent.setOnClickListener(new DialogInterface.OnClickListener() ...
答案 1 :(得分:0)
在调用findViewById之前,您应该设置对话框的内容 试试这个:
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();
}
});
}