我有两个按钮:“共享”,“继续”。两者都是在新的XML文件中创建的,因为我想让我的应用程序使用外观精美的Windows 8/10图形用户界面。
我能够显示对话框消息,但是我面临的问题是,如何检查用户单击了哪个按钮:“共享”或“继续”?我无法为他们设置onClickListener,因为此警报对话框已在新文件中创建,因此,如果我尝试这样做,它将使应用程序崩溃。
这是按钮的XML代码:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/share_button"
android:layout_marginRight="5dp"
android:text="SHARE"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:background="@drawable/button_blue" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/continue_button"
android:layout_marginLeft="5dp"
android:text="CONTINUE"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:background="@drawable/button_green" />
我将其显示为警报对话框的Java代码:
Dialog d = new Dialog(MainActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog);
d.show();
您可以通过您的dialog d
ie来调用它:
Dialog d = new Dialog(MainActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog);
Button button = (Button) d.findViewById(R.id.share_button);
Button button2 = (Button) d.findViewById(R.id.continue_button)
d.show();
然后你可以做一个正常的 onClickListener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//STUFF
}
});
希望能帮助到你 ;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句