单击通知时启动自定义对话框 [英] Launch a custom dialog when clicked on notification
问题描述
当用户单击通知栏中的通知时,我想启动一个自定义对话框.
I want to launch an custom dialog when user clicks on the notification in the notification bar.
我已经创建了通知和自定义对话框类.但是我不知道用户单击时如何启动.
I have already created the notification and the custom dialog class. But i do not know how to launch when user clicks it.
我搜索的所有教程都会启动一个Activity,而不是一个对话框.因此,任何人都可以在这方面帮助我.
All tutorials i searched launch a Activity and not a dialog. So, can anyone help me with this regard.
谢谢.
这是我的自定义对话框"代码
This is my Custom Dialog code
public class custom_dialog extends Dialog {
Context m_context;
LayoutInflater mInflater = null;
public custom_dialog (Context context, int theme) {
super(context,R.style.Theme_Dialog);
// TODO Auto-generated constructor stub
this.m_context = context;
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_SHORT).show();
mInflater=LayoutInflater.from(m_context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
推荐答案
任何单击都类似于单击元素.单击按钮即可启动自定义对话框.这是我的方法:
Well any click is like a click on an element . I launch my custom dialog on click of a button. Here's how i do it :
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Dialog" />
</LinearLayout>
custom.xml:
custom.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/image"/>/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/image"
/>
</RelativeLayout>
现在让Java绑定它们:
Now for the java to bind them :
MainActivity.java:
MainActivity.java :
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonShowCustomDialog);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Custom Title");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
您可能可以利用它来实现您的目的...
You can probably leverage this for your purpose ...
这篇关于单击通知时启动自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!