2012-02-28 80 views
0

這是我的代碼:如何編寫java代碼逐個顯示textview,例如點擊提交按鈕顯示文本視圖並存儲而下一次點擊相同的按鈕顯示下一個文本視圖和存儲data.how寫這種類型的Java代碼,我有一個主要的.xml文件如何寫代碼逐個閱讀TextView,如何存儲sd卡

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> 
+0

歡迎的Android愛好者堆棧交流。 Android Stack Exchange適用於愛好者,高級用戶和使用Android操作系統的普通用戶。關於Android開發/編程的問題在這裏是無關緊要的,請在Stack Overflow上提出這些問題。 – 2012-02-28 06:40:53

+0

好的,謝謝你,先生 – Satya 2012-02-28 06:46:54

回答

1

您可以使用列表視圖

公共類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 

} 

}

+0

我也想XML文件,我不明白 – Satya 2012-02-28 11:14:01