編輯:關於Android編程
先看布局:
main_activity.xml
result_activity.xml
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_name;
private RadioGroup rg_group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
rg_group = (RadioGroup) findViewById(R.id.radioGroup1);
}
// 點擊按鈕 實現計算人品 跳轉到ResultActivity頁面
public void click(View v) {
// [1]獲取用戶名
String name = et_name.getText().toString().trim();
// [2] 判斷一下name 是否為空
if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "親 請輸入姓名", 1).show();
return;
}
// [3]判斷用戶選擇的性別
int radioButtonId = rg_group.getCheckedRadioButtonId();//the unique id of the selected radio button in this group
int sex = 0;
switch (radioButtonId) {
case R.id.rb_male: // 代表選擇的是男
sex = 1;//1表示男性
break;
case R.id.rb_female: // 代表選擇的是女
sex = 2;
break;
case R.id.rb_other: // 代表選擇的是人妖
sex = 3;
break;
}
if(sex == 0){//哪個RadioButton也沒選
Toast.makeText(getApplicationContext(), "請選擇性別", 1).show();
return;
}
//[4]跳轉到ResultActivity頁面 用顯示意圖跳轉
Intent intent = new Intent(this, ResultActiviyt.class);
//傳遞姓名
intent.putExtra("name", name);
//傳遞性別
intent.putExtra("sex", sex);
startActivity(intent);
}
}
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ResultActiviyt extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [1]加載布局
setContentView(R.layout.activity_result);
TextView tv_name = (TextView) findViewById(R.id.tv_name);//放置姓名
TextView tv_sex = (TextView) findViewById(R.id.tv_sex);//放置性別
TextView tv_result = (TextView) findViewById(R.id.tv_result);//放置人品描述
// [2]獲取mainActivity 傳遞過來的數據
Intent intent = getIntent(); // 獲取開啟此Activity的意圖對象
// [3]獲取name 和 sex 的值 小技巧 :傳遞的是什麼數據類型 這邊就按照傳遞的數據類型取
String name = intent.getStringExtra("name");
int sex = intent.getIntExtra("sex", 0);//第二個參數值:the value to be returned if no value of the desired type is stored with the given name.
// [4]根據name 和 sex 顯示數據
tv_name.setText(name);
byte[] bytes = null;
// [5]顯示性別
try {
switch (sex) {
case 1:
tv_sex.setText("男");
//Returns a new byte array containing the characters of this string encoded using the named charset.
//同時,設置編碼是為了得到不同的二進制,目的還是為了得到不同的人品描述
bytes = name.getBytes("gbk"); //if the charset is not supported會拋異常
break;
case 2:
tv_sex.setText("女");
bytes = name.getBytes("utf-8");
break;
case 3:
tv_sex.setText("人妖");
bytes = name.getBytes("iso-8859-1");
break;
default:
break;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//[6]計算人品結果 市面上大多數應用采用的是隨機數 。下面是另一種算法
int total = 0;
for (byte b : bytes) { // 0001 1111
int number = b&0xff; // 1111 1111
total+=number;
}
System.out.println(total);//打印輸出為了看看正確性。
// 獲取得分
int score = Math.abs(total)%100;
if (score > 90) {
tv_result.setText("您的人品非常好,您家的祖墳都冒青煙啦");
}else if (score > 80) {
tv_result.setText("您的人品還可以 ");
}else if (score > 60) {
tv_result.setText("您的人品剛及格");
}else{
tv_result.setText("您的人品太次了 您需要努力啊");
}
}
}
配置文件:


Android實現仿網易今日頭條等自定義頻道listview 或者grideview等item上移到另一個view中
我這裡只是簡單的用了兩個listview來實現的,先上效果圖。比較粗糙。預留了自定義的空間。思路:從上圖應該可以看的出來。就是上下兩個listview。點擊下面的ltem
Android學習路線(三)運行你的Android應用
如果你按照上一課創建了你的Android項目,那麼它包含默認的 Hello World 的源文件,能夠讓你的項目馬上運行起來。 你如何運行你的應用以來與兩件事:你是否
Android仿微信底部菜單欄功能顯示未讀消息數量
底部菜單欄很重要,我看了一下很多應用軟件都是用了底部菜單欄,這裡使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數量的,雖然之前也做過但是layout下
Android自定義控件系列案例【四】
案例效果:模擬器上運行有些鋸齒,真機上和預期一樣好案例分析: 看效果,第一直覺肯定是Android原生態控件中沒有這樣的控件實現這種效果,自然想到應該需要自定義控件了,沒