del*_*ina 9 android button android-alertdialog
我是一个自学成才的初学者,并且非常耐心!谢谢!
在Eclipse中,我使用自己的xml文件("custom_dialog")创建了一个自定义alertdialog,它被称为"usernamealert".
如果用户尚未输入用户名(即username.length == 0),我希望弹出警报.
在这个布局中,我有一个textView("你叫什么名字?"),editText和按钮("usernameButton").
在为按钮添加onclicklistener之前,一切正常.这是我的(相关的)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();
Run Code Online (Sandbox Code Playgroud)
当我把onclicklistener放进去的时候,它破了!我应该把它放在哪里?
(以下是我曾尝试过的...所有在我的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();
}
});
Run Code Online (Sandbox Code Playgroud)
代码之后我说:
if (username.length() == 0) {
usernamealert.show();
}
Run Code Online (Sandbox Code Playgroud)
再一次,它在我开始弄乱按钮之前就已经有效了!!
需要指定代码搜索按钮的位置,如果它只在主机的xml中搜索它的"findViewById",它应该是
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);
Run Code Online (Sandbox Code Playgroud)
这是我的班级Hireteachernegotiate.class的一部分,它有一个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();
Run Code Online (Sandbox Code Playgroud)
这是对话框布局
<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>
Run Code Online (Sandbox Code Playgroud)
尝试这个。
usernamebuilder.setCancelable(false)
usernamebuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do what you want.
}
});
Run Code Online (Sandbox Code Playgroud)
看看这是否有效,或者是否有某种帮助。
| 归档时间: |
|
| 查看次数: |
7956 次 |
| 最近记录: |