我是Android开发的新手.
目前,我正在开发接收短信类应用程序,它接收短信并显示用于文本到语音功能选项的alertdialog().我在Broadcastreceiver类的另一个类中调用alertdialog()的方法.当应用程序运行时,会出现alertdialog(),但在按下Home按钮后却没有.
所以我该怎么做?
请帮我.
我有一个应用程序查询手机的数据库中的消息.如果光标返回其中包含消息,那么我试图一个接一个地在alertdialog中显示每条消息.我收到以下错误,说有窗口泄漏.
我认为这与下一个显示之前没有正确取消alertdialog有关.有谁知道我为什么会收到这个错误?
@Override
protected void onResume() {
super.onResume();
Cursor c = nfcScannerApplication.loginValidate.queryAllFromMessage();
Log.e(TAG, "messages in db = " + c.getCount());
if(c != null && c.getCount() > 0){
c.moveToFirst();
while(c.moveToNext()){
final String guid = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_GUID));
final String message = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_TEXT));
final String createdAt = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_CREATED_AT));
final String sender = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_SENDER));
DateTime dt = new DateTime(createdAt);
DateTimeFormatter fmt2 = DateTimeFormat.forPattern("H:mm d-MMM-Y");
String date = fmt2.print(dt);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
// set title
alertDialogBuilder.setTitle("You have a new message : " ); …Run Code Online (Sandbox Code Playgroud) 我使用此代码构建一个带EditText的AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(LayoutInflater.from(context).inflate(R.layout.dialog_view, null));
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();
}
});
builder.show();
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,mInput.length()== 0,因此字符串为空.mInput = ((EditText) LayoutInflater.from(context).inflate(R.layout.dialog_view, null).findViewById(R.id.etxtDialog)).getText().toString();虽然执行该行,但EditText确实包含一些字符.为什么这段代码不起作用?
我在Android应用程序中实现了警报.报警工作正常.Toast消息是可见的.
现在我想向用户发出警报框通知.
这是来自ReceiverActivityClass的代码.我试过了
public class ReceiverActivity extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Code....
new AlertDialog.Builder(context)
.setTitle("Alert Box")
.setMessage("Msg for User")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
// some coding...
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
}).create().show();
}
Run Code Online (Sandbox Code Playgroud)
}
android broadcastreceiver android-alarms android-alertdialog
我无法显示对话框,我尝试但返回空指针.问题,我认为是上下文,我已尝试过这个但获得相同的结果......我认为另一种可能性是打开一个线程并在Main Activity中显示AlertDialog,这是我的代码:
文件 Sector1.java公共类Sector1扩展Fragment {
private Button btnNew_order,btnAggiungi;
private Spinner spin_prodotto, spin_tipo;
private EditText EtQta;
ListView userList;
static UserCustomAdapter userAdapter;
ArrayList<User> userArray = new ArrayList<User>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sector1, container, false);
userAdapter = new UserCustomAdapter(getActivity().getBaseContext(), R.layout.row, userArray);
userList = (ListView) rootView.findViewById(R.id.listView);
userList.setItemsCanFocus(false);
userAdapter.notifyDataSetChanged();
userList.setAdapter(userAdapter);
Run Code Online (Sandbox Code Playgroud)
UserCustomAdapter.java
public class UserCustomAdapter extends ArrayAdapter<User> {
private static final int INVISIBLE = 4;
private static final int VISIBLE = 0;
Context context;
int layoutResourceId;
ArrayList<User> …Run Code Online (Sandbox Code Playgroud) 我没有使用负面和正面按钮.我需要关闭对话框,但dialog.dismiss()无效.
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setCancelable(false);
dialog = alert.create();
goButton.setOnClickListener(new View.OnClickListener() { //goButton is inside view which is inflated inside the dialog
@Override
public void onClick(View view) {
age = (String)spinner.getSelectedItem();
if(gender == null){
Utils.makeToast(context, "Select your gender");
}else if(age == null || age.toLowerCase().contains("age") || age.equals("")){
spinner.performClick();
}else{
Utils.makeToast(context, (String)spinner.getSelectedItem() + " - gender: " +gender);
editor.putInt("age", Utils.getIntOrZero(age));
editor.putString("gender", gender);
editor.commit();
dialog.dismiss(); // called but not working
}
}
});
alert.show();
Run Code Online (Sandbox Code Playgroud) salam
如何检查Android AlertDialog中的所有Checkbox项目(setMultiChoiceItems)
AlertDialog.Builder builder = new AlertDialog.Builder(A);
builder.setTitle(A.getString(R.string.which_number));
builder.setIcon(R.drawable.ic_launcher_mini);
builder.setMultiChoiceItems(line, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
}
});
Run Code Online (Sandbox Code Playgroud) 我有这个警报对话框,它有这两个按钮(确定和取消)。我想知道我如何实施它。
因此,当您单击取消按钮时,它应该关闭警报对话框并返回到我当前所在的片段。如果我单击“确定”按钮,它应该替换当前的警报对话框并将其放置在另一个对话框中。
这是我下面用于确认的代码。java文件;
public class confirmation extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inf = getActivity().getLayoutInflater();
final View theDIalog = inf.inflate(R.layout.example_xml, null);
builder.setView(theDIalog);
AlertDialog dialog = builder.create();
theDIalog.findViewById(R.id.makeaTransferOk).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Okay button is clicked", Toast.LENGTH_SHORT).show();
}
});
theDIalog.findViewById(R.id.makeaTransferCancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
return dialog;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的example_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:background="#ffc0c0c0"> …Run Code Online (Sandbox Code Playgroud) 如何设计带圆角和透明关闭按钮的自定义警报对话框?
android android-alertdialog material-design material-components material-components-android
//code of AlertDialog
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(R.string.modification);
persoFomulair=new ListView(getActivity());
List<HashMap<Boolean,HashMap<String,String>>>list = prepareList(p);
MyListAdapter2 myListAdapter2 = new MyListAdapter2(getActivity(), R.layout.perso_list, list);
persoFomulair.setAdapter(myListAdapter2);
alert.setView(persoFomulair);
alert.setPositiveButton(R.string.valider, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//What ever you want to do with the value
getValueOfItem();
ModifierPerso modifierPerso = new ModifierPerso();
modifierPerso.execute(type, String.valueOf(p.getId()), date_embauch, salair_journ);
}
});
alert.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
AlertDialog …Run Code Online (Sandbox Code Playgroud)keyboard android listview android-edittext android-alertdialog