Android 如何编写逐条读取的代码TextView,如何存储sd卡

Android 如何编写逐条读取的代码TextView,如何存储sd卡,android,Android,这是我的代码:如何编写java代码显示一个接一个的文本视图,例如单击submit按钮显示文本视图和存储,下次单击同一按钮显示下一个文本视图和存储数据。如何编写这种类型的java代码,我有一个main.xml文件 public class XMLRWActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInsta

这是我的代码:如何编写java代码显示一个接一个的文本视图,例如单击submit按钮显示文本视图和存储,下次单击同一按钮显示下一个文本视图和存储数据。如何编写这种类型的java代码,我有一个main.xml文件

public class XMLRWActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button submit_btn =  (Button) findViewById (R.id.submit_btn);
final EditText textbox = (EditText) findViewById (R.id.first_text);
final TextView newtext = (TextView) findViewById (R.id.read_text);

submit_btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
            byte[] readinfo = new byte[160];
            String FILENAME = "first_file";
        FileOutputStream mystream;
    try {
        mystream = openFileOutput (FILENAME, Context.MODE_PRIVATE);
        String variabletowrite = textbox.getText().toString();

        mystream.write(variabletowrite.getBytes());
        mystream.close();
        FileInputStream readstream = openFileInput (FILENAME);
        readstream.read(readinfo);
        newtext.setText(new String(readinfo));


        readstream.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
   });
   }
 }
这是一个main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<EditText android:id="@+id/first_text"
 android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" 
android:text="Submit"     
android:id="@+id/submit_btn" 
 android:layout_width="match_parent" />

<TextView
android:id="@+id/read_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView1"
android:textAppearance="?android:attr/textAppearanceLarge" ></TextView>
<TextView
android:id="@+id/read_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:textAppearance="?android:attr/textAppearanceLarge" ></TextView>
<TextView
android:id="@+id/read_text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView3"
android:textAppearance="?android:attr/textAppearanceLarge" >

 </TextView>
</LinearLayout>

这是清单文件中的存储权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission>

您可以使用listview

公共类MainActivity扩展活动实现OnClickListener、OnItemSelectedListener { /**在首次创建活动时调用*/

private static String[] data = new String[]
{ "aaaa", "bbbbbb", "ccccc", "ddddd", "eeee", "ffffff", "gggggg ", "hhhhhhhhhhh", "iiiiiiiiii", "jjjjjjjjjjj" };

private ListView lvDynamic;
private ViewAdapter viewAdapter;

private class ViewAdapter extends BaseAdapter
{
    private Context context;
    private List textIdList = new ArrayList();

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
        LinearLayout linearLayout = null;
        if (textIdList.get(position) instanceof String)
        {
            linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.text, null);
            TextView textView = ((TextView) linearLayout.findViewById(R.id.textview));
            textView.setText(String.valueOf(textIdList.get(position)));
        }
        return linearLayout;
    }

    public ViewAdapter(Context context)
    {
        this.context = context;
    }

    @Override
    public int getCount()
    {
        return textIdList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return textIdList.get(position);
    }

    public void addText(String text)
    {
        textIdList.add(text);
        notifyDataSetChanged();
    }


    @Override
    public long getItemId(int position)
    {
        return position;
    }
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id)
{
    id = position;

}

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lvDynamic = (ListView) findViewById(R.id.lvDynamic);
    Button btnAddText = (Button) findViewById(R.id.btnAddText);

    btnAddText.setOnClickListener(this);

    viewAdapter = new ViewAdapter(this);
    lvDynamic.setAdapter(viewAdapter);
    lvDynamic.setOnItemSelectedListener(this);
}


public void onClick(View v)
{
    // TODO Auto-generated method stub
    int randomNum = new Random().nextInt(data.length);
    viewAdapter.addText(data[randomNum]);
}

@Override
public void onNothingSelected(AdapterView<?> arg0)
{
    // TODO Auto-generated method stub

}
私有静态字符串[]数据=新字符串[]
{“aaaa”、“bbbbbb”、“ccccccc”、“ddddd”、“eeee”、“ffffff”、“gggg”、“hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh;
私有列表视图;
私有视图适配器;
私有类ViewAdapter扩展了BaseAdapter
{
私人语境;
私有列表textIdList=新建ArrayList();
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
字符串充气器=Context.LAYOUT\u充气器\u服务;
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(充气机);
LinearLayout LinearLayout=null;
if(textidleist.get(position)instanceof String)
{
linearLayout=(linearLayout)布局平坦。充气(R.layout.text,null);
TextView TextView=((TextView)linearLayout.findViewById(R.id.TextView));
textView.setText(String.valueOf(textidleist.get(position));
}
返回线性布局;
}
公共ViewAdapter(上下文)
{
this.context=上下文;
}
@凌驾
public int getCount()
{
返回textIdList.size();
}
@凌驾
公共对象getItem(int位置)
{
返回text idlist.get(位置);
}
公共无效添加文本(字符串文本)
{
text idlist.add(文本);
notifyDataSetChanged();
}
@凌驾
公共长getItemId(int位置)
{
返回位置;
}
}
@凌驾
已选择公共位置(AdapterView父对象、视图、整型位置、,
长id)
{
id=位置;
}
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvDynamic=(ListView)findViewById(R.id.lvDynamic);
按钮btnAddText=(按钮)findViewById(R.id.btnAddText);
btnAddText.setOnClickListener(此);
viewAdapter=新的viewAdapter(此);
lvDynamic.setAdapter(viewAdapter);
lvDynamic.setOnItemSelectedListener(此);
}
公共void onClick(视图v)
{
//TODO自动生成的方法存根
int randomNum=new Random().nextInt(data.length);
addText(数据[randomNum]);
}
@凌驾
未选择公共无效(AdapterView arg0)
{
//TODO自动生成的方法存根
}

}

欢迎Android爱好者使用Stack Exchange。Android Stack Exchange面向使用Android操作系统的爱好者、超级用户和普通用户。关于Android开发/编程的问题在这里是离题的,请问这些关于堆栈溢出的问题。我也想问xml文件,我不明白