使用filepicker选择一个文本文件并将该文本文件的内容加载到Xamarin Android中的“编辑文本”中

代码老虎

在此论坛上在此处询问了相关问题之后,非常感激能找到打开文件选择器界面的代码,以使用户能够选择MIME类型(“ .txt”)的文件,不,我需要超越此范围并加载内容将该文本文件转换为活动布局上显示的编辑文本...用于打开文件选择器界面的代码位于按钮方法内...为了简而言之,其余代码将在下面说明。

 class Notes:AppCompatActivity
    {
    //Declare this edit text variable in the class so that all methods can access it without having to redefine it
       EditText notes;
        protected override void OnCreate(Bundle onSavedInstanceState){
           //Assign the edit text
             notes = this.FindViewById<EditText>(Resource.Id.editext5);
               }
    //This button method opens the file picker interface when the button is clicked
       private void Button2_Click(object sender, EventArgs e)
        {

            //Program the open file text behavior from here
            Intent intent = new Intent();
            intent.SetType("text/plain");
            intent.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
        }
    //I had this idea to override StartActivityForResult method but am not even sure that is what i should do
         public override void StartActivityForResult(Intent intent, int requestCode)
        {
            base.StartActivityForResult(intent, requestCode);
        }
     }

可以帮助我将所选文件中存在的文本加载到我的编辑文本中的代码一定会受到赞赏,谢谢...

Leo Zhu - MSFT

选择文件后,您将获得OnActivityResult方法中的数据,您可以尝试以下方法:

class Notes:AppCompatActivity
{
//Declare this edit text variable in the class so that all methods can access it without having to redefine it
   EditText notes;
    protected override void OnCreate(Bundle onSavedInstanceState){
       //Assign the edit text
         notes = this.FindViewById<EditText>(Resource.Id.editext5);
           }
//This button method opens the file picker interface when the button is clicked
   private void Button2_Click(object sender, EventArgs e)
    {

        //Program the open file text behavior from here
        Intent intent = new Intent();
        intent.SetType("text/plain");
        intent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
    }

   protected override async void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Result.Ok)
        {
            var uri = data.Data;
            var stream = ContentResolver.OpenInputStream(uri);

            string str = "";
            StringBuffer buf = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            
            while ((str = reader.ReadLine()) != null)
            {
                buf.Append(str + "\n");
            }
            stream.Close();
            notes.Text = buf.ToString();

        }

    }
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档