2013年10月8日 星期二

各位廣大的粉絲朋友們,我菜鳥工程師又來了之 AlertDialog & EditText 除蟲記

各位廣大的粉絲朋友們,就是要打字,怎麼樣之 AlertDialog & EditText的蟲蟲是不是讓你們坐立難安,我菜鳥工程師回來了,讓你們久等了,小蟲一隻,看下去就可以輕鬆解決。

1. 建立專案,除了名稱改為EX005,其他照抄


 2. 執行程式兩次後,漏格貓的錯誤訊息,removeView()要怎麼call阿!!!



10-07 14:08:15.105: E/AndroidRuntime(636): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 

 3. Source Code



package com.example.ex005;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

     private TextView tv;
     private Button btn;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
        
         tv = (TextView) MainActivity.this.findViewById(R.id.textView1);
         btn = (Button) MainActivity.this.findViewById(R.id.button1);

         final LinearLayout ll = new LinearLayout(this);
         final EditText et = new EditText(this);

         ll.setOrientation(LinearLayout.VERTICAL);
         ll.addView(et);

         btn.setOnClickListener(new Button.OnClickListener() {

              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  new AlertDialog.Builder(MainActivity.this)
                           .setTitle("EX005")
                           .setView(ll)
                           .setPositiveButton("OK",
                                     new DialogInterface.OnClickListener() {

                                         @Override
                                         public void onClick(DialogInterface dialog,
                                                  int which) {
                                              // TODO Auto-generated method stub
                                              tv.setText(et.getText());
                                              ((ViewGroup)ll.getParent()).removeView(ll);
                                         }
                                     })
                           .setNegativeButton("Cancel",
                                     new DialogInterface.OnClickListener() {

                                         @Override
                                         public void onClick(DialogInterface dialog,
                                                  int which) {
                                              // TODO Auto-generated method stub
                                              ((ViewGroup)ll.getParent()).removeView(ll);
                                         }
                                     }).show();

              }
         });
        
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }

}


 4. 重新執行程式,熱騰騰的畫面出現了
 
5. 第一次的親密接觸

 6.恭喜你,可以再點一次

 7. 當!當!當!當!...才怪,活得好好的

 8. 世界一片美好

各位廣大的粉絲朋友們,今天就演到這裡,不用太想念我,我菜鳥工程師很快就會再出現的。

範例 : EX005

標籤: ,

0 個意見:

張貼留言

<< 首頁