setLayoutParams Android
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
2016年10月2日日曜日
Android Intent
2016/10/02
Android Intent
启动 Activity:
要启动的Activity.class
Intent intent = new intent(this, Activity.class);
要传递的数据
intent.putExtra("key", "message);
启动开始
startActivity(intent);
在被启动的Activity.class内获取数据
获取Intent
Intent intent = getIntent();
获取数据
String message = intent.getStringExtra("key");
if(getIntent().getStringExtra("key2") != null) {
TextView tv = (TextView) findViewById(R.id.textView2);
String kk = getIntent().getStringExtra("key2");
tv.setText(kk);
}
Android Intent
启动 Activity:
要启动的Activity.class
Intent intent = new intent(this, Activity.class);
要传递的数据
intent.putExtra("key", "message);
启动开始
startActivity(intent);
在被启动的Activity.class内获取数据
获取Intent
Intent intent = getIntent();
获取数据
String message = intent.getStringExtra("key");
if(getIntent().getStringExtra("key2") != null) {
TextView tv = (TextView) findViewById(R.id.textView2);
String kk = getIntent().getStringExtra("key2");
tv.setText(kk);
}
2016年10月1日土曜日
widget のレイアウトの変更
widget のレイアウトの変更
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.view.ViewGroup.*;
import static android.widget.LinearLayout.*;
import static com.example.e560.m1001a.R.id.wrap_content;
public class MainActivity extends AppCompatActivity {
public static boolean j = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
t1();
}
protected void t1(){
final LinearLayout ll = new LinearLayout(this);
final TextView tv1 = new TextView(this);
final TextView tv2 = new TextView(this);
final Button tb = new Button(this);
tv1.setBackgroundColor(Color.BLUE);
tv1.setText("" + Color.BLUE);
tv2.setBackgroundColor(Color.CYAN);
tv2.setText("" + Color.CYAN);
tb.setText("button");
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tb);
setContentView(ll);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
if(j){
j = false;
ll.setOrientation(VERTICAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}else{
j = true;
ll.setOrientation(HORIZONTAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}
setContentView(ll);
}
});
}
}
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.view.ViewGroup.*;
import static android.widget.LinearLayout.*;
import static com.example.e560.m1001a.R.id.wrap_content;
public class MainActivity extends AppCompatActivity {
public static boolean j = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
t1();
}
protected void t1(){
final LinearLayout ll = new LinearLayout(this);
final TextView tv1 = new TextView(this);
final TextView tv2 = new TextView(this);
final Button tb = new Button(this);
tv1.setBackgroundColor(Color.BLUE);
tv1.setText("" + Color.BLUE);
tv2.setBackgroundColor(Color.CYAN);
tv2.setText("" + Color.CYAN);
tb.setText("button");
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tb);
setContentView(ll);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
if(j){
j = false;
ll.setOrientation(VERTICAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}else{
j = true;
ll.setOrientation(HORIZONTAL);
tv2.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tv1.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tb.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
}
setContentView(ll);
}
});
}
}
2016年9月27日火曜日
Android Spinner
Android Spinner
Android
package com.example.java.m0926a;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = new RelativeLayout(this);
Spinner sp = new Spinner(this);
List<String> list = new ArrayList<String>();
list.add(new String("日本語"));
list.add(new String("中国語"));
list.add(new String("韓国語"));
list.add(new String("英語"));
System.out.println(list.indexOf("b"));
mya mm = new mya(MainActivity.this, list);
sp.setAdapter(mm);
int a = ViewGroup.LayoutParams.MATCH_PARENT;
int b = ViewGroup.LayoutParams.WRAP_CONTENT;
rl.addView(sp, new RelativeLayout.LayoutParams(a,b));
// rl.addView(sp);
setContentView(rl);
}
public class abc {
View v = new RelativeLayout(MainActivity.this);
}
public class mya extends BaseAdapter{
private Context context = null;
private List<String>list = null;
public mya(Context context, List<String> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(context);
TextView tx = new TextView(context);
tx.setText(list.get(position));
TextView tx2 = new TextView(context);
tx2.setText(" index = " + position);
ll.addView(tx);
ll.addView(tx2);
return ll;
}
}
}
2016年9月24日土曜日
Android Calendar
Android Calendar
TimeZone tx = TimeZone.getTimeZone("Asia/Tokyo");
Calendar d = Calendar.getInstance(tx);
または
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
TextView tv0 = (TextView) findViewById(R.id.textView);
TextView tv1 = (TextView) findViewById(R.id.textView2);
TextView tv2 = (TextView) findViewById(R.id.textView3);
TextView tv3 = (TextView) findViewById(R.id.textView4);
TextView tv4 = (TextView) findViewById(R.id.textView5);
TextView tv5 = (TextView) findViewById(R.id.textView6);
TextView tv6 = (TextView) findViewById(R.id.textView7);
TextView tv7 = (TextView) findViewById(R.id.textView8);
TextView tv8 = (TextView) findViewById(R.id.textView9);
Calendar c = Calendar.getInstance();
tv1.setText(c.getTime().toString());
tv2.setText(TimeZone.getDefault().getID().toString());
tv3.setText(String.valueOf(c.get(Calendar.HOUR_OF_DAY)));
// tv3.setText(String.valueOf(c.get(Calendar.HOUR))); tv4.setText(String.valueOf(c.get(Calendar.MINUTE)));
tv5.setText(String.valueOf(c.get(Calendar.SECOND)));
tv6.setText(String.valueOf(c.get(Calendar.YEAR)));
tv7.setText(String.valueOf(c.get(Calendar.MONTH)));
tv8.setText(String.valueOf(c.get(Calendar.DATE)));
}
TimeZone tx = TimeZone.getTimeZone("Asia/Tokyo");
Calendar d = Calendar.getInstance(tx);
または
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
TextView tv0 = (TextView) findViewById(R.id.textView);
TextView tv1 = (TextView) findViewById(R.id.textView2);
TextView tv2 = (TextView) findViewById(R.id.textView3);
TextView tv3 = (TextView) findViewById(R.id.textView4);
TextView tv4 = (TextView) findViewById(R.id.textView5);
TextView tv5 = (TextView) findViewById(R.id.textView6);
TextView tv6 = (TextView) findViewById(R.id.textView7);
TextView tv7 = (TextView) findViewById(R.id.textView8);
TextView tv8 = (TextView) findViewById(R.id.textView9);
Calendar c = Calendar.getInstance();
tv1.setText(c.getTime().toString());
tv2.setText(TimeZone.getDefault().getID().toString());
tv3.setText(String.valueOf(c.get(Calendar.HOUR_OF_DAY)));
// tv3.setText(String.valueOf(c.get(Calendar.HOUR))); tv4.setText(String.valueOf(c.get(Calendar.MINUTE)));
tv5.setText(String.valueOf(c.get(Calendar.SECOND)));
tv6.setText(String.valueOf(c.get(Calendar.YEAR)));
tv7.setText(String.valueOf(c.get(Calendar.MONTH)));
tv8.setText(String.valueOf(c.get(Calendar.DATE)));
}
登録:
投稿 (Atom)