【问题标题】:How to show the LayoutInflater in alert message?如何在警报消息中显示 LayoutInflater?
【发布时间】:2014-08-20 11:49:43
【问题描述】:

这是我在警报消息中的 layoutInflater 代码。在设备上运行时,仅出现 OK 和 CANCEL 按钮。 popup.xml(用于布局)不存在。

这是我的代码

public void onClick(View view) {
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
  LayoutInflater layoutInflater
      = (LayoutInflater) getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);
  View popupView = layoutInflater.inflate(R.layout.popup, null, false);

  alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

      Intent intObj = new Intent(getApplicationContext(),
          agree.class);
      startActivity(intObj);

    }
  });

  alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int id) {

      Intent intObj = new Intent(getApplicationContext(),
          IntentExampleActivity.class);
      startActivity(intObj);

    }

  });
  AlertDialog alertDialog = alertDialogBuilder.create();

  // show it
  alertDialog.show();
}

这是我的 popup.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="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="166dp"
    android:orientation="vertical" >
</LinearLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.12"
    android:text="By using any of the websites" />

<TextView
    android:id="@+id/more"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.12"
    android:linksClickable="true"
    android:text="More" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="match_parent"
    android:layout_height="92dp"
    android:layout_weight="0.21"
    android:text="I have read and agree to the Terms and Conditions." />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="114dp"
    android:layout_margin="20dp"
    android:orientation="vertical" >
</LinearLayout>

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    可能重复:Using LayoutInflater and AlertDialog

    您似乎正在尝试从静态 TextView 加载文本。应该是EditText吧?

    TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
    String sax1 = stax1.getText().toString();
    

    已编辑:

    试试这个代码:

    lay1.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v)        
            {                     
    
                LayoutInflater myLayout = LayoutInflater.from(context);
                final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
                alertDialogBuilder.setView(dialogView);
    
                AlertDialog alertDialog = alertDialogBuilder.create();
                Button button1 = (Button) alertDialog.findViewById(R.id.button1);
                button1.setOnClickListener(new OnClickListener(){
    
                public void onClick(View v) {
                    TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
                    String sax1 = stax1.getText().toString();
                    double sx1 = Double.parseDouble(sax1);
                    TextView textviewlay1 =(TextView)dialogView.findViewById(R.id.m1ab);
                    String stringl1 = textviewlay1.getText().toString();
                     Double doubl1 = Double.parseDouble(stringl1);      
                    TextView textviewp1 = (TextView)dialogView.findViewById(R.id.inputPrice);
                    String stringp1 = textviewp1.getText().toString();
                    Double intp1 = Double.parseDouble(stringp1);
                    double resultl1 = intp1 - (doubl1*sx1);
                    int precision21t = 100; //keep 4 digits
                    resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
                    textViewtotalproduct.setText(String.valueOf(resultl1));     
    
                }
            }); 
    
     return false;  
    
    
    
    
            }});
    

    【讨论】:

      【解决方案2】:

      试试这个

      Dialog dialog = new Dialog(YourActivtyclass.this);
      
      dialog.setContentView(R.layout.yourpopuplayout);
      
      final TextView textView1= (TextView) dialog.findViewById(R.id.textView1);
      
      dialog.show();
      

      您可以在任何地方使用它。您可以完全自定义警报对话框。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-30
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        • 2014-02-21
        • 1970-01-01
        相关资源
        最近更新 更多