2016-12-29 76 views
-4

我想在像Android一样创建一个对话框Textview如何创建在Android中具有textview的对话框?

Image]

对于创建Textview,我使用下面的函数object.settype(textviewobject)但没有成功。

+0

创建一个自定义对话框:https://www.mkyong.com/android/android-custom-dialog-example/ –

+0

您可以通过扩展DialogFragment class.http自定义警告对话框:// v4all123 .blogspot.in/2013/09/custom-dialogfragmnet-example-in-android.html –

+0

你的要求是什么 –

回答

0

使用该得到的帮助: -

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(); 
} 
0

使用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的

0
  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> 

我希望这可以帮助

0

只需使用AlertDialog.Builder,放在setMessage你的文字就会显示

 AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
     builder.setMessage("your text"); 
     builder.create(); 
     builder.show(); 
0

您可以从下面的帖子http://stackoverflow.com/questions/10903754/input-text-dialog-android

0

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(); 
相关问题