我已经创建了一个主Dialog
类,我在其上发送布局ID并将布局显示为Dialog
现在当我从调用类发送布局时它会弹出对话框但内容为对话框,即按钮无法访问我无法为它们设置点击监听器。怎么做?
打电话: -
CustomDialog obj=new CustomDialog(MailSenderActivity.this , R.layout.mydialog);
obj.show();
MAIN CLASS
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class CustomDialog extends Dialog implements
View.OnClickListener {
Dialog dialog;
int id;
public CustomDialog(MailSenderActivity mailsender, int id) {
super(mailsender);
this.id = id;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(id);
Button signInbutton=(Button)findViewById(R.id.signInButton);
Button closebutton=(Button)findViewById(R.id.closeButton);
}
public void closebutton(View v) {
Toast.makeText(getContext(), "You clicked a button!", Toast.LENGTH_LONG).show();
dismiss();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
id是: -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:padding="30dip" android:orientation="horizontal"
android:weightSum="1"
android:background="@drawable/gmail">
<LinearLayout android:layout_width="296dp"
android:orientation="vertical" android:layout_gravity="center"
android:layout_weight="1.51" android:layout_height="497dp">
<TextView android:text="My Dialog" android:textSize="24.5sp"
android:layout_width="wrap_content" android:layout_gravity="center"
android:layout_height="wrap_content" android:layout_marginBottom="25dip"></TextView>
<TextView android:text="Enter Gmail Id" android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/name" android:layout_width="358dp"
android:singleLine="true" android:layout_height="wrap_content">
</EditText>
<TextView android:text="Enter Gmail Password"
android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/name" android:layout_width="314dp"
android:singleLine="true" android:layout_height="wrap_content"
android:password="false" android:inputType="textPassword"></EditText>
<Button android:text="Sign In" android:layout_width="67dp"
android:id="@+id/signInButton" android:layout_height="wrap_content"
android:clickable="true" android:onClick="signIn"></Button>
<Button android:text="close" android:layout_width="67dp"
android:id="@+id/closeButton" android:layout_height="wrap_content"
android:clickable="true" android:onClick="closeButton"></Button>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:10)
dialog_bring_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(MyActivity.this);
dialog.setContentView(R.layout.MyCustomDialogxmlfile);
dialog.show();
/* My Cancel Button , and its listener */
my_cancel_btn=(Button)dialog.findViewById(R.id.datesetbtn);
my_cancel_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
答案 1 :(得分:9)
你去了:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(id);
Button signInbutton=(Button)findViewById(R.id.signInButton);
signInButton.setOnClickListener(this);
Button closebutton=(Button)findViewById(R.id.closeButton);
closeButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(R.id.closeButton == v.getId()) {
closeButton(v);
} else {
// do the same for signInButton
}
}
建议您事先学习基础知识。
答案 2 :(得分:4)
我发现代码更清晰,可以分离我的点击处理程序。在onCreate方法中添加:
signInbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff for signInButtonClick
}
});
// same for other button
答案 3 :(得分:1)
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
dialog_btn.setOnClickListener(new View.OnClickListener()
{
// Perform button logic
}
答案 4 :(得分:1)
在2019年为我工作。您可以使用按钮更改文本视图。
Dialog mdialog = new Dialog(this);
mdialog.setContentView(R.layout.activity_select_type);
mdialog.show();
TextView view = mdialog.findViewById(R.id.viewQuotes);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Working...", Toast.LENGTH_SHORT).show();
}
});
答案 5 :(得分:0)
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CustomDialog dialog1 = new CustomDialog(Classname.this);
dialog1.setContentView(R.layout.submitdialog);
dialog1.setTitle(" SUBMIT :");
TextView text = (TextView) dialog1.findViewById(R.id.message);
text.setText(" Your Answer is correct ");
}
}
答案 6 :(得分:0)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.warning);
builder.setTitle("Warning");
final LinearLayout backAlarmDialogLayout = (LinearLayout) getLayoutInflater()
.inflate(R.layout.custumedialog_layou, null);
builder.setView(backAlarmDialogLayout);
builder.setCancelable(true);
backAlarmDialog = builder.create();
设置这些代码以在onCreate中初始化您的服装对话框,如果您想将监听器添加到按钮,您可以编写另一种方法,如
public void onClickCancel(View view){
backAlarmDialog.dismiss();
}
然后你用
android:onClick="onClickCancel"
在XML.files中你按下对话框的按钮,我尝试了其他方法,但他们只是不行,我不知道为什么会这样。也许有人会解决我的问题。 || = =