android 自訂AlertDialog 與Activity相互傳遞資料

來源:互聯網
上載者:User

android 自訂AlertDialog 與Activity相互傳遞資料

主要實現功能:

1、從Activity的TextView中擷取字串設定到AlertDialog的TextView和EditText中

2、將AlertDialog的EditText中的值設定到Activity的TextView中

效果:

 

新手在自訂AlertDialog上的疑問筆者猜測主要有兩個:

1、自訂的layout如何放到AlertDialog中?

解答:

擷取到layout的view之後,直接調用AlertDialog.Builder的setView方法即可。

2、如何對自訂AlertDialog中的控制項進行操作?

解答:

於fragment中的操作類似,首先要擷取該layout的view,然後通過該view擷取到其中控制項進行操作。

MainActivity:

package com.example.myalertdialog;import android.app.Activity;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    TextView old_name;    Button bt_change_name;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        old_name = (TextView) findViewById(R.id.tv_name);        bt_change_name = (Button) findViewById(R.id.bt_name);        bt_change_name.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //擷取自訂AlertDialog布局檔案的view                LinearLayout change_name = (LinearLayout) getLayoutInflater()                        .inflate(R.layout.my_dialog, null);                TextView tv_name_dialog = (TextView) change_name.findViewById(R.id.tv_name_dialog);                //由於EditText要在內部類中對其進行操作,所以要加上final                final EditText et_name_dialog = (EditText) change_name.findViewById(R.id.et_name_dialog);                //設定AlertDialog中TextView和EditText顯示Activity中TextView的內容                tv_name_dialog.setText(old_name.getText().toString());                et_name_dialog.setText(old_name.getText().toString());                new AlertDialog.Builder(MainActivity.this)                        .setTitle("修改使用者名稱")                        .setView(change_name)                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which) {                                //將Activity中的textview顯示AlertDialog中EditText中的內容                                //並且用Toast顯示一下                                old_name.setText(et_name_dialog.getText().toString());                                Toast.makeText(MainActivity.this, "設定成功!", Toast.LENGTH_SHORT).show();                            }                        })                        //由於“取消”的button我們沒有設定點擊效果,直接設為null就可以了                        .setNegativeButton("取消", null)                        .create()                        .show();            }        });    }}

activity_main.xml:

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"><linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#000" android:text="原使用者名稱:">    <textview android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="大西瓜"></textview></textview></linearlayout><button android:id="@+id/bt_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改使用者名稱"></button></linearlayout>

my_dialog.xml:

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"><linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#000" android:text="原使用者名稱:">    <textview android:id="@+id/tv_name_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"></textview></textview></linearlayout>    <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">        <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#000" android:text="新使用者名稱">        <edittext android:id="@+id/et_name_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!">    </edittext></textview></linearlayout></linearlayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.