i am trying to implemet own layout on dialog window. But i don't know how to create on click listner, because i defined dialog inside another class, which has another layout. How can i "find" buttons from dialog xml in another class with another layout.
Here is code :
public void dialog(String text){
final String karta = text;
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Terminal.this);
View view = getLayoutInflater().inflate(R.layout.activity_dialog, null);
mBuilder.setView(view);
AlertDialog dialog = mBuilder.create();
dialog.show();
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String akcia = "Prichod";
pridajZaznam(karta, akcia);
}
});
b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String akcia = "Odchod prestavka";
pridajZaznam(karta, akcia);
}
});
b3 = (Button) findViewById(R.id.b3);
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String akcia = "Odchod";
pridajZaznam(karta, akcia);
}
});
b4 = (Button) findViewById(R.id.b4);
b4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String akcia = "Prichod prestavka";
pridajZaznam(karta, akcia);
}
});
}
This method is inside class Terminal which have own layout, but dialog uses activity_dialog.xml layout file. Dialog shows correctly if i dont use listners, they cause null pointer exception...
Thanks for any answers !
答案 0 :(得分:1)
Try b1 = (Button) view.findViewById(R.id.b1);
instead of b1 = (Button) findViewById(R.id.b1);
答案 1 :(得分:0)
This is how you can create a custom dialog 1. create layout like this
<LinearLayout
xmls:https://....
<TextView
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:id="@+id/myText/>
<LinearLayout
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:orientation = "horizontal"
>
<Button
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:id="@+id/positiveBtn/>
Button
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:id="@+id/negativeBtn/>
</LinearLayout>
Then call it from your Activity
Dialog myDialog = new Dialog(context);
myDialog.setContentView(R.layout.your_dilog_layout);
Button positiveBtn = myDialog.findViewById(R.id.positiveBtn);
Button negativeBtn= myDialog.findViewById(R.id.negativeBtn);
TextView tv= myDialog.findViewById(R.id.myText);
myText.setText("WWhat you want");
negativeBtn.setOnClickListenr(new OnClickListener....);
myDialog.show();