添加按键,打开或隐藏Toolbar
private void toolbarButton() {
// 找到当前的Layout
ViewGroup thislayout = (ViewGroup) findViewById(R.id.activity_main);
// 新建按键
final Button button = new Button(this);
// 添加按键到Layout
thislayout.addView(button);
// 检查按键状态,并改变按键标题
if(SUPPORTBARCHK){
button.setText(R.string.heid);
}else{
button.setText(R.string.show);
}
// 按键事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 检查按键状态,并改变按键标题,并改变ActionBar的显示状态
if(SUPPORTBARCHK) {
// getActionBar().hide(); //错误
getSupportActionBar().hide();
button.setText(R.string.show);
SUPPORTBARCHK = false;
}else{
// getActionBar().show(); //错误
getSupportActionBar().show();
button.setText(R.string.heid);
SUPPORTBARCHK = true;
}
}
});
}
2016年10月6日木曜日
2016年10月5日水曜日
EmptyActivite 基础上添加Toolbar
EmptyActivite 基础上添加Toolbar
MainActivity.java
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// EmptyActivite 基础上添加Toolbar
// step1,AppCompatActivity
// step2,noActionBar manifest
// step3,activitymain.xml
// step4, new Toolbar
Toolbar tb = (Toolbar) findViewById(R.id.my_toolbar);
tb.setTitle("newToolbar");
setSupportActionBar(tb);
// 背景图标 or 背景颜色
tb.setBackgroundResource(R.drawable.firefox);
tb.setBackgroundResource(android.R.color.holo_blue_bright);
// 添加先上导航,在ActionBar上添加
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
// 测试
// ab.hide();
}
// 添加ToolBar 的选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif = new MenuInflater(this);
mif.inflate(R.menu.optmenu, menu);
menu.add("addMenu1");
menu.add("addMenu2");
System.out.println(menu.size());
return super.onCreateOptionsMenu(menu);
}
// ToolBar 选项菜单事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("item");
System.out.println(item.getTitle());
System.out.println(item.getItemId());
System.out.println(item.getMenuInfo());
return super.onOptionsItemSelected(item);
}
}
MainActivity.java
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// EmptyActivite 基础上添加Toolbar
// step1,AppCompatActivity
// step2,noActionBar manifest
// step3,activitymain.xml
// step4, new Toolbar
Toolbar tb = (Toolbar) findViewById(R.id.my_toolbar);
tb.setTitle("newToolbar");
setSupportActionBar(tb);
// 背景图标 or 背景颜色
tb.setBackgroundResource(R.drawable.firefox);
tb.setBackgroundResource(android.R.color.holo_blue_bright);
// 添加先上导航,在ActionBar上添加
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
// 测试
// ab.hide();
}
// 添加ToolBar 的选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif = new MenuInflater(this);
mif.inflate(R.menu.optmenu, menu);
menu.add("addMenu1");
menu.add("addMenu2");
System.out.println(menu.size());
return super.onCreateOptionsMenu(menu);
}
// ToolBar 选项菜单事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("item");
System.out.println(item.getTitle());
System.out.println(item.getItemId());
System.out.println(item.getMenuInfo());
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.java.m1004c">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.java.m1004c.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
optmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:icon="@android:drawable/ic_dialog_email"
android:title="menu1.title"
android:showAsAction="ifRoom"
/>
<item
android:id="@+id/menu2"
android:title="menu2.title"
android:showAsAction="always"
/>
</menu>
2016年10月2日日曜日
setLayoutParams Android
setLayoutParams Android
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
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)