我想在像Android一樣創建一個對話框Textview
:如何創建在Android中具有textview的對話框?
對於創建Textview
,我使用下面的函數object.settype(textviewobject)
但沒有成功。
我想在像Android一樣創建一個對話框Textview
:如何創建在Android中具有textview的對話框?
對於創建Textview
,我使用下面的函數object.settype(textviewobject)
但沒有成功。
使用該得到的幫助: -
private void fn_showAlertDialog() {
new AlertDialog.Builder(YourActivity.this)
.setTitle("Title of your dialog")
.setMessage("Text that you want to show.")
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do your task
dialog.cancel();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do your task
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
使用alertdialog這樣,創造對話的另一個佈局:
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") final View alertlayout = inflater.inflate(R.layout.notif_msg_dialog, null);
TextView text_main=(TextView)alertlayout.findViewById(R.id.notif_msg);
text_main.setText("YOUR TEXT");
builder.setView(alertlayout);
packsizeDialog=builder.create();
packsizeDialog.show();
這裏,notif_msg_dialog.xml是具有TextView的
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
TextView text1 =
(TextView)dialog.findViewById(R.id.text1);
Button proceed =
(Button)dialog.findViewById(R.id.button);
proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
dialog.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="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Proceed"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
我希望這可以幫助
只需使用AlertDialog.Builder,放在setMessage你的文字就會顯示
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("your text");
builder.create();
builder.show();
您可以從下面的帖子http://stackoverflow.com/questions/10903754/input-text-dialog-android
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="@android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />
<Button
android:id="@+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
你必須擴展對話框和工具OnClickListener
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}
如何調用呢?
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
創建一個自定義對話框:https://www.mkyong.com/android/android-custom-dialog-example/ –
您可以通過擴展DialogFragment class.http自定義警告對話框:// v4all123 .blogspot.in/2013/09/custom-dialogfragmnet-example-in-android.html –
你的要求是什麼 –