Opening a text input dialog in view mode on Android

I have an application with a SurfaceHolder based view (similar to the Lunar Lander tutorial). The entire GUI is drawn on the canvas, and I want you to be able to request user text input at the moment, using a special dialog dialog, which is then processed and displayed on the canvas using the standard procedure.

My problem, however, is that it is best to open dialog boxes from Activity. This is also not a problem, because I thought I could create a handler and then pass it to a view, which, in turn, can use it to send messages from the GUI stream in the "Activity" view, which, in turn, could get an input, and send a response back, etc.

The problem is that after calling setContentView(R.layout.main) , which contains the entire application, I want to call MyAppView mMyAppView = (MyAppView) findViewById(R.id.app_view_id) .

This call returns null .

What is considered best practice here? I can’t find any good examples, and the API appears, well, not so many.

I would appreciate any help here.

+4
source share
2 answers

Create a thematic dialog action to display the 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:

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

then open onActivityResult answer

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

A manifest is defined as:

 <activity android:name=".utils.TextEntryActivity" android:label="Type in the value" android:theme="@android:style/Theme.Dialog" /> 
+14
source

I also want to answer this question for myself. The answer is already good. Android developer page in the samples change dialog box.

I have not read it fully yet, but if you are looking for a tag

DIALOG_TEXT_ENTRY

this is similar to what you (and me) need.

I will first examine this example.

0
source

Source: https://habr.com/ru/post/1313074/


All Articles