• <i id='sNxk8'><tr id='sNxk8'><dt id='sNxk8'><q id='sNxk8'><span id='sNxk8'><b id='sNxk8'><form id='sNxk8'><ins id='sNxk8'></ins><ul id='sNxk8'></ul><sub id='sNxk8'></sub></form><legend id='sNxk8'></legend><bdo id='sNxk8'><pre id='sNxk8'><center id='sNxk8'></center></pre></bdo></b><th id='sNxk8'></th></span></q></dt></tr></i><div id='sNxk8'><tfoot id='sNxk8'></tfoot><dl id='sNxk8'><fieldset id='sNxk8'></fieldset></dl></div>

    <small id='sNxk8'></small><noframes id='sNxk8'>

    <tfoot id='sNxk8'></tfoot>
      <legend id='sNxk8'><style id='sNxk8'><dir id='sNxk8'><q id='sNxk8'></q></dir></style></legend>
        <bdo id='sNxk8'></bdo><ul id='sNxk8'></ul>
      1. 从 Android 的视图中打开带有文本输入的对话框

        时间:2023-08-30

            <i id='juSrX'><tr id='juSrX'><dt id='juSrX'><q id='juSrX'><span id='juSrX'><b id='juSrX'><form id='juSrX'><ins id='juSrX'></ins><ul id='juSrX'></ul><sub id='juSrX'></sub></form><legend id='juSrX'></legend><bdo id='juSrX'><pre id='juSrX'><center id='juSrX'></center></pre></bdo></b><th id='juSrX'></th></span></q></dt></tr></i><div id='juSrX'><tfoot id='juSrX'></tfoot><dl id='juSrX'><fieldset id='juSrX'></fieldset></dl></div>
          1. <legend id='juSrX'><style id='juSrX'><dir id='juSrX'><q id='juSrX'></q></dir></style></legend>

            • <small id='juSrX'></small><noframes id='juSrX'>

                  <tbody id='juSrX'></tbody>

              • <tfoot id='juSrX'></tfoot>
                  <bdo id='juSrX'></bdo><ul id='juSrX'></ul>

                  本文介绍了从 Android 的视图中打开带有文本输入的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个基于 SurfaceHolder 的带有 View 的应用(类似于 Lunar Lander 教程).整个 GUI 绘制在画布上,我希望能够在给定时刻使用自定义布局对话框提示用户文本输入,然后使用标准程序处理并呈现到画布上.

                  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 text input at a given moment using a custom layout Dialog, that is then taken care of and rendered to the canvas using standard procedure.

                  然而,我的问题是,似乎最好的做法是从活动中打开对话框.这也没有问题,因为我认为我可能会创建一个处理程序,然后将其传递给视图,视图又可以使用它将消息从视图中的 GUI 线程传递到 Activity,进而可以获取输入,并发送回复等.

                  My problem, however, is that it seems that best practice is to open Dialogs from the Activity. This is no problem either, since i thought i might create a Handler and then pass it to the View that could in turn use it to pass Messages from the GUI thread in the View on to the Activity, that in turn could fetch the input, and send a reply back, etc.

                  问题是,在我调用包含整个应用程序的 setContentView(R.layout.main) 之后,我想调用 MyAppView mMyAppView = (MyAppView) findViewById(R.id.app_view_id).

                  Problem is, after I call setContentView(R.layout.main), which contains the whole app, i want to call MyAppView mMyAppView = (MyAppView) findViewById(R.id.app_view_id).

                  此调用返回 null.

                  这里认为最佳做法是什么?我找不到任何好的示例,而且 API 正在出现,嗯,不多.

                  What is considered to be best practice here? I can't find any good examples and the API is turning up, well, not much.

                  如果有任何帮助,我将不胜感激.

                  I would appreciate any help here.

                  推荐答案

                  创建一个以对话框为主题的活动以显示在您当前的活动上.

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

                  发布者:

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

                  然后在 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;
                          }
                      }
                  

                  清单定义为:

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

                  这篇关于从 Android 的视图中打开带有文本输入的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在Android上将值从Dialog传回Activity的可靠方法? 下一篇:如何关闭 AlertDialog.Builder?

                  相关文章

                  最新文章

                    <bdo id='SSJsw'></bdo><ul id='SSJsw'></ul>
                • <legend id='SSJsw'><style id='SSJsw'><dir id='SSJsw'><q id='SSJsw'></q></dir></style></legend>

                  <small id='SSJsw'></small><noframes id='SSJsw'>

                • <tfoot id='SSJsw'></tfoot>

                  1. <i id='SSJsw'><tr id='SSJsw'><dt id='SSJsw'><q id='SSJsw'><span id='SSJsw'><b id='SSJsw'><form id='SSJsw'><ins id='SSJsw'></ins><ul id='SSJsw'></ul><sub id='SSJsw'></sub></form><legend id='SSJsw'></legend><bdo id='SSJsw'><pre id='SSJsw'><center id='SSJsw'></center></pre></bdo></b><th id='SSJsw'></th></span></q></dt></tr></i><div id='SSJsw'><tfoot id='SSJsw'></tfoot><dl id='SSJsw'><fieldset id='SSJsw'></fieldset></dl></div>