**主要實現功能:**
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:
~~~
<?xml version="1.0" encoding="utf-8"?>
<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="大西瓜" />
</LinearLayout>
<Button
android:id="@+id/bt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改用戶名"
/>
</LinearLayout>
~~~
my_dialog.xml:
~~~
<?xml version="1.0" encoding="utf-8"?>
<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!" />
</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!"
/>
</LinearLayout>
</LinearLayout>
~~~
源碼地址:[http://download.csdn.net/detail/double2hao/9411213](http://download.csdn.net/detail/double2hao/9411213)
- 前言
- android Nine-Patch的使用(制作聊天界面必學)
- android 圖片文字輪播效果(圖片和文字自動滾動)
- LinearLayout布局中如何讓控件置底
- viewpager+將activity轉化成view 做主界面(可點擊可滑動,超容易理解的demo)
- android swipeRefreshLayout 下拉刷新 google官方組件
- android 自定義AlertDialog 與Activity相互傳遞數據
- android 簡單地設置Activity界面的跳轉動畫
- android XML動畫初步解析(activity界面之間跳轉demo)
- android selector設置button點擊效果(詳細)以及常見問題
- android 用java動態設置布局(增添刪除修改布局)