我遇到了麻烦。当你点击一个项目时会出现一个对话框,显示"cambiar.xml"
中的内容,我想在按“OK”或“Borrar”时关闭对话,我尝试了一切,但我不能,这是代码!
public class Iniciado extends Activity{
TextView tv3;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iniciado);
tv3=(TextView)findViewById(R.id.tv3);
Bundle bolsar = getIntent().getExtras();
tv3.setText("Bienvenido "+ bolsar.getString("NombreKey"));
listView = (ListView)findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
Dialog d = new Dialog(Iniciado.this);
d.setTitle("Editar evento");
d.setContentView(R.layout.cambiar);
d.show();
}
});
}
对不起我的英文,谢谢!
我终于解决了它,这是具有打开对话框的按钮的类的代码!
package com.example.iniciosesion;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class Iniciado extends Activity {
TextView tv3;
ListView listView;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iniciado);
tv3=(TextView)findViewById(R.id.tv3);
Bundle bolsar = getIntent().getExtras();
tv3.setText("Bienvenido "+ bolsar.getString("NombreKey"));
listView = (ListView)findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
// Make Dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.cambiar);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
EditText editar = (EditText) dialog.findViewById(R.id.eteditar);
Button dialogButton = (Button) dialog.findViewById(R.id.okbtn);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
以下是对话框布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
<EditText
android:id="@+id/eteditar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/eteditar" >
<Button
android:id="@+id/okbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK" />
<Button
android:id="@+id/borrarbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Borrar" />
</LinearLayout>
</RelativeLayout>
谢谢!
答案 0 :(得分:0)
您需要初始化Dialog布局中的两个按钮,以便在对话框布局内执行按钮单击操作:
Button btn_OK = (Button) d.findViewById(R.id.dialog_btn_ok);
btn_OK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// close Dialog here
d.dismiss();
}
});
同样适用于Borrar
按钮