Skip to content

Instantly share code, notes, and snippets.

@shobotch
Created December 30, 2013 08:23
Show Gist options
  • Save shobotch/8179318 to your computer and use it in GitHub Desktop.
Save shobotch/8179318 to your computer and use it in GitHub Desktop.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="追加" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_frame" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
package ws.temp.dustcode;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends Activity {
private EditText[] editText = new EditText[4];
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
editText[0] = (EditText) findViewById(R.id.editText1);
editText[1] = (EditText) findViewById(R.id.editText2);
editText[2] = (EditText) findViewById(R.id.editText3);
editText[3] = (EditText) findViewById(R.id.editText4);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String[] text = new String[4];
for (int i = 0; i < text.length; i++)
text[i] = editText[i].getText().toString();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putStringArray("text", text);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
package ws.temp.dustcode;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
private ArrayAdapter<String> adapter;
private Spinner spinner;
private ArrayList<String[]> list = new ArrayList<String[]>();
private int mainFrame;
private TextView[] textView = new TextView[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainFrame = R.id.main_frame;
textView[0] = (TextView) findViewById(R.id.textView1);
textView[1] = (TextView) findViewById(R.id.textView2);
textView[2] = (TextView) findViewById(R.id.textView3);
textView[3] = (TextView) findViewById(R.id.textView4);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
set(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("add").setIcon(android.R.drawable.ic_input_add)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
startActivityForResult(new Intent(this, AddActivity.class), 0);
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String text[] = data.getExtras().getStringArray("text");
list.add(text);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length; i++)
sb.append(text[i]);
adapter.add(sb.toString());
Log.d(TAG, sb.toString());
}
private void set(int position) {
String[] text = list.get(position);
for (int i = 0; i < text.length; i++) {
textView[i].setText(text[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment