溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android應用中監(jiān)聽Dialog窗體

發(fā)布時間:2020-11-26 15:58:34 來源:億速云 閱讀:258 作者:Leah 欄目:移動開發(fā)

怎么在Android應用中監(jiān)聽Dialog窗體?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

test.class代碼

package com.test;
import Android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class test extends Activity implements OnClickListener {
  private ImageButton button=null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button=(ImageButton)findViewById(R.id.button);
    //監(jiān)聽
    button.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    Windows();
  }
  private void Windows() {
  //接受一些參數(shù)如:圖片,標題,正文
  AlertDialog.Builder builder;
  AlertDialog alertDialog;
  //加載和訪問資源
  Context mContext =test.this;
  //通過LayoutInlater得到上面xml布局的View view
  LayoutInflater inflater =
  (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  View layout =inflater.inflate(R.layout.custom_dialog,null);
  //綁定test和image
  TextView text=(TextView)layout.findViewById(R.id.text);
  text.setText("hello");
  Button b =(Button)layout.findViewById(R.id.b);
  b.setText("butto");
  b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      System.out.println("123");
    }
  });
  ImageView image=(ImageView)layout.findViewById(R.id.image);
  //取出系統(tǒng)自帶的圖片
  image.setImageResource(R.drawable.icon);
  //加載
  builder = new AlertDialog.Builder(mContext);
  builder.setView(layout);
  //創(chuàng)建目標,這里創(chuàng)建相應的AlertDialog
//  alertDialog = builder.create();
  //彈出窗口
  builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog,
     int which) {
    }
    }).setNegativeButton("取消",
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog,
     int which) {
     dialog.cancel();
    }
    }).create();
  builder.show();
  }
}

custom_dialog.xml文件代碼

<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >
 <ImageView android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp"
    />
 <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"
    />
    <Button android:id="@+id/b"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    />
</LinearLayout>

main.xml代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
  <ImageButton
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#000000"
  android:src="@drawable/icon"
  />
</LinearLayout>

結(jié)尾:由于要用到兩個xml所以這里有兩個xml的代碼,一個是點擊按鈕的xml代碼,一個窗體里面的xml布局帶

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI