我正在玩一个简单的应用程序,我想在点击按钮时显示警报.
我到目前为止的代码是:
package max.helloworld.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
private Button closeButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setContentView(R.layout.main);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setKeyListener(new onClickListener() {
@Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Transformers");
alertDialog.setMessage("Optimus Prime");
alertDialog.setButton("OK", new OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
});
//alertDialog.setIcon(R.drawable.icon);
alertDialog.show(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码创建列表对话框
new AlertDialog.Builder(ContactActivity.this)
.setTitle(contactStore.getContactName())
.setItems(contactStore.getContactNumber(),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
Run Code Online (Sandbox Code Playgroud)
但它以下列形式显示数据

但我想创造这样的东西

所以为了实现这一点,我需要创建自定义对话框并在其中加载listview吗?
我正在尝试制作一个半透明的Dialog,以便您可以在开始之前在后台看到游戏.
我已经看过这篇关于如何让它变得透明的文章,但是没有什么真正的半透明或者是否可能.
我想要的有点像他提供的图片,但解决方案给出的是对话框完全消失的背景.
非常感谢!
我正在开发一个有ListView... 的对话框
我已成功将适配器,选择器,list_itemlayout放入onCreate()如下..
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.variable_dialog);
lv=(ListView) findViewById(R.id.VariableslistView1);
lv.setAlwaysDrawnWithCacheEnabled(true);
final ArrayAdapter<String> ad=new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,Program.get().symboltable.getSymbols());
lv.setAdapter(ad);
lv.setSelector(R.drawable.item_selected);
Run Code Online (Sandbox Code Playgroud)
但每当我试图得到getSelectedItemPosition()它时,它总是返回-1
好像我没有选择任何东西..即使我如此调用我自己的选择:
lv.setSelected(true);
lv.setSelection(1);
lv.setItemChecked(1, true);
ad.notifyDataSetChanged();
ad.notifyDataSetInvalidated();
Run Code Online (Sandbox Code Playgroud)
这是我的选择器:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="2dp"
android:color="#FF00FF00" />
<gradient
android:angle="225"
android:endColor="#DD2ECCFA"
android:startColor="#DD000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
为了上帝的缘故,到底是什么错误的?!?!
我甚至试过AdapterView.OnItemSelectedListener但从未成功:|
任何人都可以告诉我这是什么问题.
提前致谢
android android-arrayadapter android-listview android-dialog
我有下一个代码:
dateDisplay1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
option = 1;
showDialog(DATE_DIALOG_ID);
}
});
Run Code Online (Sandbox Code Playgroud)
和onCreateDialog函数:
protected Dialog onCreateDialog(int id) {
Calendar cal = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dayDate, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeDate, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是将不推荐使用的showDialog()调整为DialogFragment,但没有成功.下面的代码有效但不是最佳实践.所以我想纠正代码.
你应该如何实现呢?
我写了一个自定义对话框,它工作得很好.
public class GalleryAlertDialog extends Dialog {}
Run Code Online (Sandbox Code Playgroud)
在课堂上,我用班级来称呼班级
dialog.show();
Run Code Online (Sandbox Code Playgroud)
我希望实现这个目标:

我需要对话框上的按钮,就像这样.
但是,这就是我得到的:

这是布局xml的部分,我在其中声明布局和其中的按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_buttonContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dip"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_button_popupclose"
android:contentDescription="TODO"
android:tag="btn_close" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_pagerContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_buttonContainer"
android:layout_marginTop="80dip"
android:background="@color/white" />
<ToggleButton
android:id="@+id/tbFavorite"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_gravity="bottom|right"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dip"
android:background="@drawable/selector_check"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ff212121"
android:gravity="center_vertical"
android:orientation="horizontal" > …Run Code Online (Sandbox Code Playgroud) android transparent android-layout android-alertdialog android-dialog
我的应用程序在后台,我使用的服务在满足某个条件时发送广播:
if(send)
{
Intent intent = new Intent(Tags.MSG_BROADCAST_ID);
intent.putExtra(Tags.MESSAGE, msg);
Log.w(Tags.DEBUG,"Broadcast");
sendBroadcast(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
我的广播接收器获得广播,它应该显示一个警告对话框.我用这个:
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null && Globals.MainActivity != null)
{
msg = bundle.getString(Tags.MESSAGE);
Globals.MainActivity.ShowMessage(msg);
}
}
Run Code Online (Sandbox Code Playgroud)
当我的主要活动位于前台时,警报会正确显示.当它在后台时,什么都看不见.
Runnable runnable = new Runnable()
{
public void run()
{
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("My_App");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
wl.acquire();
AlertDialog.Builder alertDialogBuilder = new …Run Code Online (Sandbox Code Playgroud) 点击DialogFragment时我会被显示出来TextView.它DialogFragment有自己的XML布局文件,a ListView,a EditText和Button底部.
问题是,当ListView只有少数几个项目时,DialogFragment仍占据屏幕的大部分(即高度远大于应有的高度).
这是我的意思的截图(黑色是ListView一个项目):
正如你所看到的那样,ListView当我希望Dialog它只是它需要的大小时,就会有大量的空白空间.
这是DialogFragentXML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_small"
tools:context="com.cohenadair.anglerslog.fragments.ManageFragment">
<LinearLayout
android:id="@+id/add_item_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/new_item_edit"
android:layout_weight="1"
android:hint="@string/hint_new_item"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_add"
android:id="@+id/add_button"
android:layout_margin="0dp"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/add_item_layout"
android:id="@+id/content_list_view"
android:background="@android:color/black">
</ListView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
而且onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_manage_primitive, …Run Code Online (Sandbox Code Playgroud) android listview android-listview android-dialog android-dialogfragment
我有一个活动,其中包含一个工具栏中的图像,以及一个活动,其主题就像一个具有相同图像的对话框.这些是布局文件:
my_activity.xml:
...
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/my_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop"
android:src="@drawable/my_drawable"
android:transitionName="@string/my_image_transition"
app:riv_oval="true" />
...
Run Code Online (Sandbox Code Playgroud)
my_dialog_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@id/my_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/my_drawable"
android:tint="@color/accent"
android:transitionName="@string/my_image_transition" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
通过在清单中应用此主题,对话框活动变为对话框:
<style name="MyDialogActivity" parent="Theme.AppCompat.Light.Dialog.MinWidth">
<item name="windowNoTitle">true</item>
<item name="android:windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
活动和对话框看起来都是我希望它们看起来的样子.
现在我想用这些ImageView实现共享元素动画:当用户点击第一个活动的图像时,它必须增长并移动到屏幕中间,以便它适合更大的对话框图像.
所以我写了这段代码:
Intent intent = new Intent(MyActivity.this, MyDialogActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
MyActivity.this, v, getString(R.string.my_image_transition)
);
startActivityForResult(intent, 999, options.toBundle());
} else {
startActivityForResult(intent, 999);
}
Run Code Online (Sandbox Code Playgroud)
动画确实发生了,但移动的图像仅在对话框区域内可见.因此,您会看到来自对话框左上角的图像,但您没有看到它从对话框外的第一个活动移动.
这是一个HTML版本,它应该是什么样子以及它目前的样子:https://jsfiddle.net/wutqdh9d/1/
我的自定义对话框中有一个时间选择器。结果是:
这是我的代码:
<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">#FFFFFF</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowNoTitle">true</item>
</style>
final Dialog dialog = new Dialog(SabadKharid_s1.this, R.style.DialogStyler2);
dialog.setContentView(R.layout.dialogtime);
timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.show();
dialog.getWindow().setAttributes(lp);
Run Code Online (Sandbox Code Playgroud)
如您所见,textView标题中的小时和分钟为白色。如何更改其背景颜色?