点击button,弹出一个带EditText的对话框,可以更改button的text名字

java:

 
  
package com.dialog.test2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DialogTest2 extends Activity {
/** Called when the activity is first created. */
private Button btn;
private String TAG = " DialogTest1 " ;
public String newName;

// 可以把这个变量定义成类级的,也可以定义在方法里面,如果定义在方法里面,局部内部类又想访问的话,只能定义成final类型的了
// public EditText mname_edit;

/*
* LayoutInflater factory = LayoutInflater.from(this); View textEntryView =
* factory.inflate(R.layout.myedit, null);
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG,
" $$$$onCreate... " );
btn
= (Button) findViewById(R.id.button1);
btn.setOnClickListener(mylistener);
// 也不能把对View对定义在这,跟定义在onResume里面不行是一样的道理。
// LayoutInflater factory = LayoutInflater.from(this);
// View textEntryView = factory.inflate(R.layout.myedit, null);
}
private OnClickListener mylistener = new OnClickListener(){

@Override
public void onClick(View v) {
Log.v(TAG,
" $$$$onClick " );
// 按了button按钮以后,让他弹出一个重命名对话框
rename();
}
};

public void rename() {
Log.v(TAG,
" $$$$$$$$$$$$$inrename.. " );
// layout = (LinearLayout) getLayoutInflater().inflate(R.layout.myedit,
// null);
LayoutInflater factory = LayoutInflater.from( this );
View textEntryView
= factory.inflate(R.layout.myedit, null );
// removeView();
// 内部局部类,只能访问方法的final类型的变量
final EditText mname_edit = (EditText) textEntryView
.findViewById(R.id.rename_edit);
// 不是用这个方法获取EditText的内容的
// mname_edit.addTextChangedListener(mTextWatcher);

// create a dialog
new AlertDialog.Builder(DialogTest2. this )
.setIcon(R.drawable.icon)
.setTitle(R.string.title_name)
.setView(textEntryView)
.setNegativeButton(R.string.btn2_name,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub

}

})
.setPositiveButton(R.string.btn1_name,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// dialog.
// TODO Auto-generated method stub
Log.v(TAG, " 你点击了确定 " );
if ( ! mname_edit.getText().toString().equals( "" )) {
newName
= mname_edit.getText().toString();
}
// newName = mname_edit.getText().toString();
btn.setText(newName);
Log.v(TAG,
" $$$$$btn.setText(newName); " );
}

}).show();

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super .onResume();
Log.v(TAG,
" $$$$onResume... " );
// 不要把对话框的view放到这里!这样的话,下次再点击button试图更改名字的时候,就会提示有两个view存在,会出错。
/* LayoutInflater factory = LayoutInflater.from(this);
textEntryView = factory.inflate(R.layout.myedit, null);
*/
// 把Button的监听放到这,与放到onCreate里面都可以,因为虽然后来弹出了一个新对话框,但是这个activity视图仍然还是显示的!
/* btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(mylistener);
*/

}
// 下面可以看见在这个activity里面用这种方式弹出一个对话框,整个activity的生命周期的运行情况。
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super .onDestroy();
Log.v(TAG,
" $$$$$onDestroy " );
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super .onPause();
Log.v(TAG,
" $$$$$onPause() " );
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super .onRestart();
Log.v(TAG,
" $$$$$onRestart() " );
}

@Override
protected void onStart() {
// TODO Auto-generated method stub
super .onStart();
Log.v(TAG,
" $$$$$onstart() " );
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super .onStop();
Log.v(TAG,
" $$$$$onStop " );
}


}

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
="点我更改名字"
/>
< Button
android:id ="@+id/button1"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="按我会显示对话框更改名字哦!" />
</ LinearLayout >

myedit.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
="10dip"
android:gravity
="center_horizontal" >
< TextView
android:id ="@+id/rename_message"
android:layout_height
="wrap_content"
android:layout_width
="wrap_content"
android:textSize
="20sp"
android:layout_gravity
="left"
android:textAppearance
="?android:attr/textAppearanceMedium"
android:text
="@string/rename_msg_input"
/>
< EditText
android:id ="@+id/rename_edit"
android:layout_height
="match_parent"
android:layout_width
="match_parent"
android:textSize
="18sp"
android:scrollHorizontally
="true"
android:autoText
="false"
android:capitalize
="none"
android:textAppearance
="?android:attr/textAppearanceMedium"
android:singleLine
="true" />
</ LinearLayout >

如果把View的定义放到上面注明的其他地方,当第二次点击button更改名字的时候,会出现这个错误:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

用现在的代码,这样每次点击都可以更改名字了。

转载于:https://www.cnblogs.com/snowdrop/articles/2043080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值