Opening a Dialog with text input from within a View in Android

后端 未结 2 1600
日久生厌
日久生厌 2020-12-28 11:24

I have an app with a View based on the SurfaceHolder (similar to the Lunar Lander tutorial). The whole GUI is drawn on a canvas, and I want to be able to prompt for user tex

相关标签:
2条回答
  • 2020-12-28 11:44

    I am also looking to answer this question for myself. The answer already here is a good one. The android developer page as alter dialog samples.

    I haven't read it completely yet, but if you search for the tag

    DIALOG_TEXT_ENTRY

    this would seem to be what you (and I) need.

    I'm going to investigate that example first.

    0 讨论(0)
  • 2020-12-28 11:58

    Create a dialog themed activity to display over your current activity.

    public class TextEntryActivity extends Activity {
        private EditText et;
    
        /*
         * (non-Javadoc)
         * @see android.app.Activity#onCreate(android.os.Bundle)
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_text_entry);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                    WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            // title
            try {
                String s = getIntent().getExtras().getString("title");
                if (s.length() > 0) {
                    this.setTitle(s);
                }
            } catch (Exception e) {
            }
            // value
    
            try {
                et = ((EditText) findViewById(R.id.txtValue));
                et.setText(getIntent().getExtras().getString("value"));
            } catch (Exception e) {
            }
            // button
            ((Button) findViewById(R.id.btnDone)).setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    executeDone();
                }
            });
        }
    
        /* (non-Javadoc)
         * @see android.app.Activity#onBackPressed()
         */
        @Override
        public void onBackPressed() {
            executeDone();
            super.onBackPressed();
        }
    
        /**
         *
         */
        private void executeDone() {
            Intent resultIntent = new Intent();
            resultIntent.putExtra("value", TextEntryActivity.this.et.getText().toString());
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
        }
    
    
    }
    

    Launch by:

    Intent foo = new Intent(this, TextEntryActivity.class);
    foo.putExtra("value", "old value to edit");
    this.startActivityForResult(foo, EDIT_ACTION);
    

    then catch the response on onActivityResult

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case EDIT_ACTION:
                    try {
                        String value = data.getStringExtra("value");
                        if (value != null && value.length() > 0) {
                            //do something with value
                        }
                    } catch (Exception e) {
                    }
                    break;
                default:
                    break;
            }
        }
    

    Manifest is defined as:

    <activity
                android:name=".utils.TextEntryActivity"
                android:label="Type in the value"
                android:theme="@android:style/Theme.Dialog" />
    
    0 讨论(0)
提交回复
热议问题