私のダイアログボックスは、1つのタイトルと2つのボタンを持つカスタムレイアウトdialogbox_solutionです。これがメインラインのみのXMLファイルです:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<TextView
android:id="@+id/txtDiaMsg"
/>
<TableLayout
android:layout_below="@+id/txtDiaMsg">
<TableRow>
<Button
android:id="@+id/yesButton"
android:text=" YES "/>
<Button
android:id="@+id/noButton"
android:text=" NO "/>
</TableRow>
</TableLayout>
</RelativeLayout>
カスタムのポジティブボタンとネガティブボタンをプログラムでリンクするにはどうすればよいですか?
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
// do something here
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater li = getLayoutInflater();
View view = li.inflate(R.layout.dialogbox_solution, null);
builder.setView(view);
builder.show();
このようなものを作成します...最初にレイアウトxmlファイルを作成します...例:dialog.xml ...次に、必要に応じて以下のコードのように呼び出します...
final Dialog myDialog = new Dialog(this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.dialog);
myDialog.setCancelable(false);
Button yes = (Button) myDialog.findViewById(R.id.share);
Button no = (Button) myDialog.findViewById(R.id.no);
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do your code here
}
});
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do your code here
}
});
myDialog.show();
myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.cancel();
return true;
}
return false;
}
});
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#e7e7e9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:id="@+id/textView10"
android:textSize="20sp"
android:textColor="#000000"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="120dp"
android:layout_height="40dp"
android:text="@string/yes"
android:id="@+id/share"
android:background="@drawable/loginbutton"
android:textSize="12sp"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="left|bottom"
android:layout_marginTop="30dp"
android:layout_below="@+id/textView15"
android:layout_alignLeft="@+id/textView15"
android:layout_alignStart="@+id/textView15" />
<Button
android:layout_width="120dp"
android:layout_height="40dp"
android:text="@string/no"
android:id="@+id/no"
android:layout_gravity="bottom|right"
android:background="@drawable/loginbutton"
android:textSize="12sp"
android:gravity="center_vertical|center_horizontal"
android:layout_alignTop="@+id/share"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加