将文本放入动态生成的编辑文本中

时间:2013-07-15 10:51:35

标签: android

我创建了管理员和用户登录页面。在用户登录页面中,我动态创建了编辑框。现在我还创建了一个警告对话框,其中包含一个编辑文本。现在,如果我在警告对话框编辑框中键入文本,则应在动态生成的编辑框中显示相同的文本。

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


     EditText input = new EditText(this);
    Context Context = this;
    AlertDialog Alert=new AlertDialog.Builder(Context).setTitle("Add Field").setMessage("Proceed").setView(input).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            Toast.makeText(getApplicationContext(), "You clicked on Yes", Toast.LENGTH_LONG).show();

        }
    }).setNegativeButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            dialog.cancel();
            Toast.makeText(getApplicationContext(), "You clicked on No", Toast.LENGTH_LONG).show();


        }
    }).show();


        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.editTextGroupLayout);
        EditText editTextView = new EditText(this);
        editTextView.setGravity(Gravity.CENTER);

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

        editTextView.setLayoutParams(params);
        editTextView.setHint("Enter Field ");
        linearLayout.addView(editTextView);

        editTextView.setInputType(InputType.TYPE_NULL);

        Log.v("EditText", etextra.getText().toString());


 if(v.getId()==R.id.button_selectoptionsback){


            Intent a=new Intent(this,Admin.class);
            this.startActivity(a);
    }    

}

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码。

package com.example.test;

import android.os.Bundle;

import android.app.Activity;

import android.app.Dialog;

import android.text.Editable;

import android.text.TextWatcher;

import android.view.Menu;

import android.view.ViewGroup.LayoutParams;

import android.widget.EditText;

import android.widget.LinearLayout;

public class MainActivity extends Activity {



EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    editText = new EditText(this);
    editText.setLayoutParams(new LayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)));
    Dialog dialog = new Dialog(this);
    dialog.setTitle("Dialog");
    dialog.setContentView(R.layout.activity_main);
    EditText dialogEditText = (EditText) dialog.findViewById(R.id.editText);
    dialogEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            editText.setText("" + s);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
    dialog.show();

    setContentView(linearLayout);
}

@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;
}

}