我想创建自定义对话框。我在其中显示了两个按钮是和否。我想在我的MainActivity.java活动中设置这两个按钮的onclicklistener事件,但这两个按钮的布局不同,所以我该怎么做?
这是我的代码。
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B = (Button) findViewById(R.id.button1);
final Button B1 = (Button) findViewById(R.id.btn1);
final Button B2 = (Button) findViewById(R.id.btn2);
final Dialog d = new Dialog(MainActivity.this);
B.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.setContentView(R.layout.dialog);
d.setTitle("This is custom dialog box");
d.setCancelable(true);
d.show();
}
});
}
这是我在MainActivity.java类中充气的activity_main 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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
但我的两个按钮是在dialog.xml布局中。
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lion" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Hey there i am lion"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:layout_weight="0.50"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:layout_weight="0.50"
android:layout_marginLeft="100dp"
/>
</RelativeLayout>
</LinearLayout>
所以我如何在MainActivity.java类中设置btn1和btn2 onclicklistener事件。
答案 0 :(得分:2)
在onClick(....)
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog, null);
d.setContentView(view);
Button btn1= (Button) view.findViewById(R.id.btn1);
Button btn2= (Button) view.findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your job
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your job
}
});
答案 1 :(得分:0)
因为按钮btn1
和btn2
都在dialog.xml
布局中,所以要访问这两个按钮,您应该使用对话框上下文:
Dialog d;
private void showDialog(){
d = new Dialog(MainActivity.this);
d.setContentView(R.layout.dialog);
d.setTitle("This is custom dialog box");
d.setCancelable(true);
d.show();
final Button B1 = (Button)d. findViewById(R.id.btn1);
final Button B2 = (Button)d. findViewById(R.id.btn2);
}
现在,班级showDialog()
在按钮点击
答案 2 :(得分:0)
尝试以下代码: -
// add listener to button
buttonClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}
});
见以下链接: -
答案 3 :(得分:0)
您需要实施以下更改:
public class MainActivity extends ActionBarActivity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
....
B.setOnClickListener(this);
....
}
@Override
public void OnClick(View v) {
int id = v.getId();
switch(id) {
case R.id.btn1:
... // Your button code here.
break;
}
}
}