2011-09-07 47 views
7

Tôi đang tạo một hộp thoại giống như một màn hình đăng nhập có chứa hai hộp văn bản và hai nút. Tôi có thể tạo ra nó nhưng vấn đề của tôi là hai hộp văn bản chỉnh sửa được chồng chéo với nhau (hộp văn bản chỉnh sửa thứ hai trùng lặp với hộp văn bản thứ nhất). Nó có thể là một trong những đơn giản nhưng kể từ khi i m mới cho android i m bị mắc kẹt với nó. Xin giúp tôi để giải quyết nó. Đây là mã nguồnĐặt nhiều hộp văn bản trong hộp thoại trong android

public class LoginActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 

    public void onCreate(Bundle savedInstanceState) {       
     super.onCreate(savedInstanceState);       
     setContentView(R.layout.main); 
     Button btn= (Button) findViewById(R.id.btn_Login); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       showDialog(0); 

      } 
     }); 
    } 

     protected Dialog onCreateDialog(int id) 
     { 
     final AlertDialog.Builder alert = new AlertDialog.Builder(this);       
     final EditText input = new EditText(this); 
     final EditText input1 = new EditText(this); 
     alert.setIcon(R.drawable.icon); 
     alert.setTitle("Login"); 
     alert.setView(input); 
     alert.setView(input1); 

     alert.setView(input1); 
     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {    
      public void onClick(DialogInterface dialog, int whichButton) {    
       String value = input.getText().toString().trim();      
       Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); 
       }      });     
     alert.setNegativeButton("Cancel",     
       new DialogInterface.OnClickListener() {       
      public void onClick(DialogInterface dialog, int whichButton) {   
       dialog.cancel(); }  });   
     return alert.create();  
     } 
    } 
+0

Tôi muốn đi để làm màn hình đăng nhập một Hoạt động riêng biệt và xác định bố trí trong xml - nếu đây là một tùy chọn cho bạn – DonGru

Trả lời

4

Bạn cũng có thể tạo bố cục XML cho hộp thoại. Trước khi gọi điện thoại của bạn chỉ cần làm:

myDialog.setContentView(R.layout.my_dialog_layout); 
17

gì nếu bạn thử với một LinerLayout:

LinearLayout lila1= new LinearLayout(this); 
lila1.setOrientation(LinearLayout.VERTICAL); 
final EditText input = new EditText(this); 
final EditText input1 = new EditText(this); 
lila1.addView(input); 
lila1.addView(input1); 
alert.setView(lila1); 

Như thế này:

public class LoginActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 

public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.main); 
    Button btn= (Button) findViewById(R.id.btn_Login); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      showDialog(0); 

     } 
    }); 
} 

    protected Dialog onCreateDialog(int id) 
    { 
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    LinearLayout lila1= new LinearLayout(this); 
    lila1.setOrientation(1); //1 is for vertical orientation 
    final EditText input = new EditText(this); 
    final EditText input1 = new EditText(this); 
    lila1.addView(input); 
    lila1.addView(input1); 
    alert.setView(lila1); 

     alert.setIcon(R.drawable.icon); 
     alert.setTitle("Login"); 

     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {    
      public void onClick(DialogInterface dialog, int whichButton) {    
       String value = input.getText().toString().trim();      
       Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); 
       }      });     
     alert.setNegativeButton("Cancel",     
       new DialogInterface.OnClickListener() {       
      public void onClick(DialogInterface dialog, int whichButton) {   
       dialog.cancel(); }  });   
     return alert.create();  
     } 
    } 

Và nó hoạt động hoàn hảo: Tôi mời các bạn copy paste nó :).

+1

Buộc đóng ứng dụng của tôi nếu tôi cố gắng bao gồm bố cục tuyến tính bằng hộp thoại. – SSG

+0

Bạn làm tôi nghi ngờ sự tỉnh táo của chính mình :), tôi tự mình thử mã mới ở trên ... thử nó: 2 phút đầu. Lúc đầu, tôi quên bình luận một trong 3 alert.setView và tôi cũng bị ép buộc. – mthpvg

+1

Đây chỉ là những gì tôi đang tìm kiếm. Cảm ơn bạn. – Hama

0

người đàn ông thnx, đó là công việc !!! nhưng tôi trong trường hợp này nó cho tôi một errror, vì vậy tôi thay đổi điều này:

lila1.setOrientation(1); //1 is for vertical orientation 

về điều này:

lila1.setOrientation(LinearLayout.VERTICAL); 
Các vấn đề liên quan