Category Archives: Android

「Andorid」Fragment画面を作成する方法

Javaコード

public class ViewPagerFragmentDemo extends Fragment {
private String  text;
private TextView tvw = null;

public ViewPagerFragmentDemo(String text){
super();
this.text = text;
}

/**
*inflater inflate()でviewを返却
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View ww = inflater.inflate(R.layout.view_pager_fragment_demo1, container, false);
tvw = (TextView)v.findViewById(R.id.viewPagerText);
tvw.setText(text);
return ww;
}
}

「Android」drawable-Selectorの使い方

ファイル例(XMLコード)
<?xml version=”1.0″ encoding=”utf-8″?>
<selector xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:state_selected=”true” android:drawable=”@drawable/button_bg_press” />
<item android:state_focused=”true” android:drawable=”@drawable/button_bg_press” />
<item android:state_pressed=”true” android:drawable=”@drawable/button_bg_press”  />
<item android:drawable=”@drawable/button_bg_normol”  />
</selector>

Javaコード
<Button
android:id=”@+id/bt_about”
style=”@style/Button_style”
android:background=”@drawable/button_selector”
android:layout_width=”250dp”
android:layout_height=”50dp”
android:layout_margin=”5dp”
android:textColor=”@color/test_color_selector”
android:text=”@string/about” />

「Android」requestWindowFeatureの使い方

Javaコード
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULL_SCREEN, WindowManager.LayoutParams.FLAG_FULL_SCREEN);
setContentView(new MyView(this));
}
}

「Android」画面の向きを設定するコード

AndroidManifest.xml
http://schemas.android.com/apk/res/Android”
< package=”com.oa”
android:versionCode=”1″
android:versionName=”1.0″>
<android:label=”@string/app_name”
android:screenOrientation=”portrait”>

「Android」getWindow().setFlags()の使い方

Javaコード
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

「Android」画像ファイルパスを取得するメモ

Javaコード
public static String getRealFilePath( final Context context, final Uri uri ) {
if ( null == uri ) return null;
final String scheme = uri.getScheme();
String data = null;
if ( scheme == null )
data = uri.getPath();
else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
data = uri.getPath();
} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null );
if ( null != cursor ) {
if ( cursor.moveToFirst() ) {
int index = cursor.getColumnIndex( ImageColumns.DATA );
if ( index > -1 ) {
data = cursor.getString( index );
}
}
cursor.close();
}
}
return data;
}

[Android入門]ボタン(Button)を使用する方法

1.一つボタン
public class DemoActivity extends Activity {
private Button button = (Button) findViewById(R.id.button_id);

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}

2.複数ボタン
public class DemoActivity extends Activity implements OnClickListener {

private Button Btn1, Btn2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//UI initialization
setupUI();
}
public void setupUI() {
Btn1 = (Button) findViewById(R.id.button1);
Btn2 = (Button) findViewById(R.id.button2);

Btn1.setOnClickListener(this);
Btn2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {

case R.id.button1:
onFn1();
break;
case R.id.button2:
onFn2();
break;
}

}

[Android入門]RadioGroupのラジオボタンの使い方

Androidコード:
m_RadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
m_RadioGroup.clearCheck();

m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup group, int checkedId) {
String answer = question.getAnswer();
if(m_Radiocft.getText().toString().subSequence(0, 1).equals(answer)){
m_Radiocft.setChecked(true);
str=”chuming success!”;
}else{
str=”chuming error!”;
}
m_TextViewanswer.setText(“right answer:”+answer+”>>>>”+str);
}
} );

Android開発–通信用ライブラリVolleyを利用するサンプルコード

1.Gradle
dependencies {
compile ‘com.github.bumptech.glide:volley-integration:1.0.+’
compile ‘com.mcxiaoke.volley:library:1.0.+’
}

dependencies {
compile ‘com.github.bumptech.glide:volley-integration:1.0.+’
compile ‘com.mcxiaoke.volley:library:1.0.+’
}

2.Maven
<dependency>
<groupId>com.github.bumptech.glide</groupId>
<artifactId>volley-integration</artifactId>
<version>1.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.mcxiaoke.volley</groupId>
<artifactId>library</artifactId>
<version>1.0.5</version>
<type>aar</type>
</dependency>

<dependency>
<groupId>com.github.bumptech.glide</groupId>
<artifactId>volley-integration</artifactId>
<version>1.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.mcxiaoke.volley</groupId>
<artifactId>library</artifactId>
<version>1.0.5</version>
<type>aar</type>
</dependency>

3.ActivityにVolleyを使う
public void onCreate() {
Glide.get(this).register(GlideUrl.class, InputStream.class,
new VolleyUrlLoader.Factory(yourRequestQueue));

}

public void onCreate() {
Glide.get(this).register(GlideUrl.class, InputStream.class,
new VolleyUrlLoader.Factory(yourRequestQueue));

}

Android入門–ファイルを読込み、書き出しをする方法

1.ファイルを読込み
ByteArrayOutputStream stream=new ByteArrayOutputStream();

FileInputStream finos= openFileInput(“demofile.txt”);
byte[] buffer=new byte[1024];
int len=-1;
while((len=inStream.read(buffer))!=-1) {
stream.write(buffer,0,length);
}

stream.close();
cfs.close();
text.setText(stream.toString());
2.ファイルを書き出し
FileOutputStream fos = openFileOutput(“demofile.txt”,MODE_APPEND);
fos.write(text.getText().toString().getBytes());
fos.close();

3 / 2012345...1020...最後 »