OnClickListener里面定制的Android alertdialog [英] OnClickListener inside custom alertdialog Android
问题描述
我是一个自学成才的初学者和AP preciate耐心!谢谢!
I am a self-taught beginner and appreciate patience! Thanks!
在Eclipse中,我做了一个自定义的alertdialog有自己的XML文件(CUSTOM_DIALOG),它被称为usernamealert。
In Eclipse, I made a custom alertdialog with its own xml file ("custom_dialog") and it is called "usernamealert".
我想警报弹出,如果用户没有输入用户名,但(即username.length == 0)。
I want an alert to pop-up if the user hasn't entered a username yet (ie, username.length == 0).
这里面的布局我有一个TextView(你叫什么名字?),EDITTEXT和按钮(usernameButton)。
Inside this layout I have a textView ("What is your name?"), editText and button ("usernameButton").
将在onclicklistener的按钮之前,一切正常。这是我的(相关)的Java:
Before putting in the onclicklistener for the button, everything worked. This was my (relevant) Java:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);
AlertDialog usernamealert = usernamebuilder.create();
当我把onclicklistener的,它打破了!应该在哪里我已经把它?
When I put the onclicklistener in, it broke! Where should I have put it?
(以下是我试过......都在我的OnCreate)
(the following is what I had tried... all in my OnCreate)
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);
Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
usernameButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//store username in sharedprefs
usernamealert.dismiss();
}
});
之后,code我说:
After the code I said:
if (username.length() == 0) {
usernamealert.show();
}
此外,它的工作之前,我开始与按钮!
Again, it worked before I started messing with the button!!
推荐答案
这是需要以指定将在code搜索按钮,如果只有findViewById,将在主机的XML搜索,它应
It is needed to specify where will the code search the button, if its only "findViewById" it would search in the xml of the host, it should be
LayoutInflater inflater =getLayoutInflater();
View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(myview);
Button addS = (Button) myview.findViewById (R.id.bAddS);
这是我的课,Hireteachernegotiate.class,其中有hireteachernegotiate.xml的布局的一部分
This is part of my class, Hireteachernegotiate.class, which has a layout of hireteachernegotiate.xml
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater =getLayoutInflater();
View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(myview);
Button addS = (Button) myview.findViewById (R.id.bAddS);
addS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do some stuff
}
});
Button minusS = (Button) myview.findViewById (R.id.bMinusS);
addS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do other stuff
}
});
// Add action buttons
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
这是对话框布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bAddS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/bMinusS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
这篇关于OnClickListener里面定制的Android alertdialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!