我想在我的代码中使用对话框做一些事情,我对此事有疑问。我的问题发生在这里:
btnDialogCancel.setOnClickListener(new View.OnClickListener()
任何人都可以帮助我吗?很多
这是我的MainActivity类:
public class MainActivity extends AppCompatActivity {
private ProgressBar prgChronemeter;
private TextView txtShowSecond;
private Handler handler = new Handler();
private Button btnSelectMin;
private Button btnDialogSelect;
private Button btnDialogCancel;
private EditText etMinute;
private Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prgChronometer = (ProgressBar) findViewById(R.id.prgChronometer);
txtShowSecond = (TextView) findViewById(R.id.txtShowSecond);
btnSelectMin = (Button) findViewById(R.id.btnSelectMin);
btnSelectMin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialogCalistir();
}
});
}
private void customDialogCalistir(){
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
etMinute = (EditText) findViewById(R.id.etMinute);
btnDialogCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
btnDialogSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
这是我的dialog.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnDialogSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="Seç"
android:layout_marginStart="42dp"
android:layout_marginTop="155dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_centerHorizontal="true"
android:id="@+id/etMinute"
android:layout_marginTop="50dp"
/>
<Button
android:id="@+id/btnDialogCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginEnd="12dp"
/>
</RelativeLayout>
答案 0 :(得分:1)
private void customDialogCalistir(){
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
etMinute = (EditText) dialog.findViewById(R.id.etMinute);
btnDialogCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
btnDialogSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
使用它将解决您的问题。
答案 1 :(得分:0)
btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
etMinute = (EditText) findViewById(R.id.etMinute);
你不能直接使用findViewByid方法直接引用对话框元素。请使用此
,而不是使用对话框的元素 btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
etMinute = (EditText) dialog.findViewById(R.id.etMinute);