String content from dialog popup feeding into EditText field
我正在尝试创建一个功能,用户在弹出对话框中输入名称,然后输入的名称会自动输入到下一页的 EditText 字段中,有点像请求标题名称,然后进行下一页的标题。对话框弹出部分正在工作,但我在将存储在 \\'m_Text\\' 字符串变量中的值放入 EditText 字段时遇到问题。请有人帮帮我!!!
这是调用对话框的页面的代码......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.evicapture.loadsave; //import com.example.camerademo.R; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.InputType; import android.view.View; import android.widget.Button; import android.widget.EditText; public class IntroMenu extends Activity { public static String m_Text =""; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.intro); Button next = (Button) findViewById(R.id.NewCase); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(IntroMenu.this); builder.setTitle("Insert Case Name/No:"); final EditText input = new EditText(IntroMenu.this); input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL); builder.setView(input); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { m_Text = input.getText().toString(); Intent myIntent = new Intent("com.evicapture.loadsave.CASEINFO"); startActivityForResult(myIntent, 0); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } }); } |
以及它与 EditText 字段一起经过的字段的代码....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.evicapture.loadsave; //import com.example.camerademo.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class caseInfo extends Activity { EditText etCaseNameNo; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.case_info_form); etCaseNameNo=(EditText)findViewById(R.id.tvCaseNameNo); etCaseNameNo.setText(IntroMenu.m_Text); Button next = (Button) findViewById(R.id.Back1); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent myIntent = new Intent(view.getContext(),IntroMenu.class); startActivityForResult(myIntent, 0); } }); } } |
如果还需要什么,请询问..我是android编程的新手,并且正在努力,所以请善待..
谢谢!
你必须通过意图传递 m_text(String)。
为此,您可以使用:
putExtra:向意图添加扩展数据。
1 2 3 4 5 6 | final String MESSAGE ="com.evicapture.loadsave.username"; m_Text = input.getText().toString(); Intent myIntent = new Intent("com.evicapture.loadsave.CASEINFO"); myIntent.putExtra(MESSAGE, strName); startActivityForResult(myIntent, 0); |
对于检索,您可以使用:
1 2 3 4 5 6 7 | extras = getIntent().getExtras(); if(extras == null) { newString= null; } else { newString= extras.getString(MESSAGE); } etCaseNameNo.setText(newString); |