Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Xamarin Android中,使用filepicker选择文本文件并将该文本文件的内容加载到编辑文本中_Android_Xamarin_Text_Filepicker - Fatal编程技术网

在Xamarin Android中,使用filepicker选择文本文件并将该文本文件的内容加载到编辑文本中

在Xamarin Android中,使用filepicker选择文本文件并将该文本文件的内容加载到编辑文本中,android,xamarin,text,filepicker,Android,Xamarin,Text,Filepicker,在这个论坛上问了一个相关问题之后,我非常感谢找到了打开文件选择器界面的代码,让用户能够选择mime类型的文件(“.txt”),不,我需要超越这一点,并将该文本文件的内容加载到我活动版面上的编辑文本中。。。 打开文件选择器界面的代码位于一个按钮方法中……长话短说,下面将解释我的其余代码 class Notes:AppCompatActivity { //Declare this edit text variable in the class so that all methods

在这个论坛上问了一个相关问题之后,我非常感谢找到了打开文件选择器界面的代码,让用户能够选择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);
        }
     }
课堂笔记:AppCompatActivity
{
//在类中声明此编辑文本变量,以便所有方法都可以访问它,而无需重新定义它
编辑文本注释;
受保护的重写void OnCreate(Bundle onSavedInstanceState){
//指定编辑文本
notes=this.findviewbyd(Resource.Id.editext5);
}
//此按钮方法在单击按钮时打开文件选择器界面
私有无效按钮2\u单击(对象发送者,事件参数e)
{
//从这里编程打开文件文本行为
意图=新意图();
intent.SetType(“文本/普通”);
intent.SetAction(intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent,“选择文件”),1);
}
//我有这个想法来覆盖StartActivityForResult方法,但我甚至不确定这是我应该做的
public override void StartActivityForResult(Intent-Intent,int-requestCode)
{
base.StartActivityForResult(意图、请求代码);
}
}

如果有代码可以帮助我将所选文件中的文本加载到编辑文本中,我将不胜感激,谢谢…

选择文件后,您将在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();

        }

    }
 }
课堂笔记:AppCompatActivity
{
//在类中声明此编辑文本变量,以便所有方法都可以访问它,而无需重新定义它
编辑文本注释;
受保护的重写void OnCreate(Bundle onSavedInstanceState){
//指定编辑文本
notes=this.findviewbyd(Resource.Id.editext5);
}
//此按钮方法在单击按钮时打开文件选择器界面
私有无效按钮2\u单击(对象发送者,事件参数e)
{
//从这里编程打开文件文本行为
意图=新意图();
intent.SetType(“文本/普通”);
intent.SetAction(intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent,“选择文件”),1);
}
受保护的重写异步void OnActivityResult(int requestCode,[GeneratedEnum]结果代码,意图数据)
{
base.OnActivityResult(请求代码、结果代码、数据);
if(requestCode==1&&resultCode==Result.Ok)
{
var uri=data.data;
var stream=ContentResolver.OpenInputStream(uri);
字符串str=“”;
StringBuffer buf=新的StringBuffer();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(流));
而((str=reader.ReadLine())!=null)
{
追加(str+“\n”);
}
stream.Close();
notes.Text=buf.ToString();
}
}
}