當前位置:首頁 » 安卓系統 » android代碼button

android代碼button

發布時間: 2022-08-05 07:31:03

① 用Android開發,在代碼中動態生成Button按鈕,能設置這個按鈕的坐標嗎,怎麼設置呢

設置按鈕坐標,需要上一層是絕對布局,然後在用 button.setLayoutParams(params);設置button出現的位置

② android 代碼中如何設置button坐標 同時又如何得到現有button的坐標

你可以直接在繪畫界面先大概將你的button按鈕放好,然後回到代碼界面,找到button的坐標,微調
一般現有的button的坐標是根據的布局來看,例如你選得是線性布局,要麼是橫著排過來,要麼就是豎著,看你的線性布局的方向屬性是什麼!

安卓軟體Button退出代碼怎麼寫

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
exitBy2Click(); //調用雙擊退出函數
}
return false;
}
/**
* 雙擊退出函數
*/
private static Boolean isExit = false;

private void exitBy2Click() {
Timer tExit = null;
if (isExit == false) {
isExit = true; // 准備退出
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
tExit = new Timer();
tExit.schele(new TimerTask() {
@Override
public void run() {
isExit = false; // 取消退出
}
}, 2000); // 如果2秒鍾內沒有按下返回鍵,則啟動定時器取消掉剛才執行的任務

} else {
finish();
System.exit(0);
}
}

④ 安卓代碼中,我有五張圖片,設置一個button,如何點擊一次button就切換下一張圖片

第一種:使用動畫的方法實現:(代碼繁瑣)
這種發放需要:兩個動畫效果,一個布局,一個主類來實現,不多說了,來看代碼吧:
public class IamgeTrActivity extends Activity {
/** Called when the activity is first created. */

public ImageView imageView;
public ImageView imageView2;

public Animation animation1;
public Animation animation2;

public TextView text;

public boolean juage = true;

public int images[] = new int[] { R.drawable.icon, R.drawable.expriment,
R.drawable.changer, R.drawable.dataline, R.drawable.preffitication };

public int count = 0;

public Handler handler = new Handler();

public Runnable runnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView 出去 imageView2 進來
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setBackgroundResource(images[count % 5]);
count++;
imageView2.setBackgroundResource(images[count % 5]);

text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i(handler, handler);
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
//將iamgeView先隱藏,然後顯示
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}

public void onPause() {
juage = false;
super.onPause();
}
}

布局代碼:

android:orientation=vertical
android:layout_width=fill_parent
android:layout_height=fill_parent
android:id=@+id/rl>

android:id=@+id/imageView
android:layout_width=fill_parent
android:background=@drawable/icon
android:layout_below=@+id/rl
android:layout_height=120dp />

android:id=@+id/imageView2
android:layout_width=fill_parent
android:background=@drawable/expriment
android:layout_below=@+id/rl
android:layout_height=120dp />

android:id=@+id/text
android:layout_width=fill_parent
android:layout_height=wrap_content
android:layout_below=@id/imageView/>

第二種:使用ViewFlipper實現圖片的輪播

Android系統自帶的一個多頁面管理控制項,它可以實現子界面的自動切換:
首先 需要為ViewFlipper加入View
(1) 靜態導入:在layout布局文件中直接導入
(2) 動態導入:addView()方法
ViewPlipper常用方法:
setInAnimation:設置View進入屏幕時候使用的動畫
setOutAnimation:設置View退出屏幕時候使用的動畫
showNext:調用該函數來顯示ViewFlipper裡面的下一個View
showPrevious:調用該函數來顯示ViewFlipper裡面的上一個View
setFlipInterval:設置View之間切換的時間間隔
startFlipping使用上面設置的時間間隔來開始切換所有的View,切換會循環進行
stopFlipping:停止View切換
講了這么多,那麼我們今天要實現的是什麼呢?
(1) 利用ViewFlipper實現圖片的輪播
(2) 支持手勢滑動的ViewFlipper
我們需要先准備幾張圖片:把圖片放進drawable中
創建兩個動畫:在res下面新建一個folder裡面新建兩個xml:
left_in:

android:ration=5000
android:fromXDelta=100%p
android:toXDelta=0/>

left_out:

android:fromXDelta=0
android:toXDelta=-100%p
android:ration=5000/>

一個布局文件:
xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_parent
android:layout_height=match_parent
tools:context=.MainActivity >

android:id=@+id/flipper
android:layout_width=fill_parent
android:layout_height=fill_parent/>

一個主類:
public class MainActivity extends Activity {

private ViewFlipper flipper;
private int[] resId = {R.drawable.pc1,R.drawable.pc2,R.drawable.pc3,R.drawable.pc4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

flipper = (ViewFlipper) findViewById(R.id.flipper);

/*
* 動態導入的方式為ViewFlipper加入子View
* */
for (int i = 0; i < resId.length; i++) {
flipper.addView(getImageView(resId[i]));

}
/*
* 為ViewFlipper去添加動畫效果
* */
flipper.setInAnimation(this, R.anim.left_in);
flipper.setOutAnimation(this, R.anim.left_out);
flipper.setFlipInterval(5000);
flipper.startFlipping();
}
private ImageView getImageView(int resId){
ImageView image = new ImageView(this);
image.setBackgroundResource(resId);
return image;
}

}
那麼這樣就實現了一個圖片輪詢的功能效果了
我們還可以添加點擊,滑動效果:
我們還需要添加兩個向右的滑動效果:
right_in:

android:fromXDelta=0
android:toXDelta=-100%p
android:ration=2000/>

right_out:

android:fromXDelta=100%p
android:toXDelta=0
android:ration=2000/>

然後我們還需要在主類裡面添加(如果你不想讓圖片自動播放,只想通過手勢來實現圖片播放那麼你需要把「為ViewFlipper添加動畫效果的代碼」刪掉):
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
break;
case MotionEvent.ACTION_MOVE://判斷向左滑動還是向右滑動
if (event.getX() - startX > 100) {
flipper.setInAnimation(this, R.anim.left_in);
flipper.setOutAnimation(this, R.anim.left_out);
flipper.showPrevious();
}else if (startX - event.getX() > 100) {
flipper.setInAnimation(this, R.anim.right_in);
flipper.setOutAnimation(this, R.anim.right_out);
flipper.showNext();
}

case MotionEvent.ACTION_UP:
break;

}
return super.onTouchEvent(event);
}
這樣我們利用我們的ViewFlipper完成的圖片輪詢的功能就做完了。
午夜神器APP私密即時語音互動聊天,匿名兩性情趣秘密分享

⑤ android開發 button鏈接代碼

先初始化控制項添加點擊事件,

java">button.setonclicklistener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(ActivityA.this,ActivityB.class);
satrtActivity(intent);
}
});

⑥ android studio中的button有哪些方法

先介紹下修改原理:首先打開位於android.widget包下面的Button.java文件,這里有一句關鍵的代碼如下:
public
Button(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
com.android.internal.R.attr.buttonStyle);
}
1
2
3
其中com.android.internal.R.attr.buttonStyle就是我們修改樣式的關鍵了,網上的教程的修改方法大都是:
<Button
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:text="價格"
/>
1
2
3
4
5
6
也就是在對應的xml裡面button控制項裡面編寫style達到目的。
但是如果我們的app需要完全統一整個應用的button的樣式,那麼就需要在每一個button裡面添加style。
這顯然效率太低下了。
接下來打開我們項目中values文件夾下面的styles.xml文件,我們創建安卓項目的時候,會有一個默認的styles文件。
打開之後找到這段代碼:
<style
name="AppBaseTheme"
parent="Theme.Holo.Light">
<!--
Theme
customizations
available
in
newer
API
levels
can
go
in
res/values-vXX/styles.xml,
while
customizations
related
to
backward-compatibility
can
go
here.
-->
</style>
<!--
Application
theme.
-->
<style
name="AppTheme"
parent="AppBaseTheme">
1
2
3
4
5
6
7
8
9
10
不保證讀者的默認styles.xml和我的是一樣的,不過大概是這個樣子,有可能讀者的最低支持是2.3、那麼就沒有Them.Light。
我們使用eclipse的快捷鍵打開這個Theme.Holo.Light。可以看到如下代碼:
<style
name="Theme.Holo.Light"
parent="Theme.Light">
<item
name="colorForeground">@android:color/bright_foreground_holo_light</item>
<item
name="colorForegroundInverse">@android:color/bright_foreground_inverse_holo_light</item>
<item
name="colorBackground">@android:color/background_holo_light</item>
<item
name="colorBackgroundCacheHint">@android:drawable/background_cache_hint_selector_holo_light</item>
<item
name="disabledAlpha">0.5</item>
<item
name="backgroundDimAmount">0.6</item>

⑦ android開發中button按鈕不起作用,btn不響應點擊事件,檢查沒有發現代碼的問題。

你既然已經 findViewById了你的button了,你又去實例化一個btn,那這樣你的btn其實是你new的那個,但是你在你的Activity上看到的是你findViewById的那個Button,你new的btn由於你沒設置他的位置,所以你看不到!

⑧ android 如何設置button的大小

在相對應的layout.xml文件中,button下可以定義android:layout_width和android:layout_height兩個屬性可以用來控制button的大小,希望可以幫助到你!

⑨ android開發中點擊button無反應大神來看看這個代碼的問題在哪裡

把 button_listener = new Button.OnClickListener() {
public void onClick(View v) {
//setTitle("哎呦,button被點了一下");
Intent intent=new Intent(MainActivity.this,edText.class);
startActivity(intent);
} 這段放到 onCreate 方法外面

Button.OnClickListener button_listener = new Button.OnClickListener() {
public void onClick(View v) {
//setTitle("哎呦,button被點了一下");
Intent intent=new Intent(MainActivity.this,edText.class);
startActivity(intent);
}

⑩ android 用代碼實現以下形狀的button,不能用imagebutton

方法一:使用Button,然後再使用android:background
方法二:使用ImageButton,然後再使用android:src
都能達到你要的效果

熱點內容
c語言編譯庫文件 發布:2024-05-22 01:33:46 瀏覽:563
調音里的壓縮 發布:2024-05-22 01:33:39 瀏覽:569
android紅外遙控 發布:2024-05-22 01:31:47 瀏覽:872
伺服器分區如何分兩個主機 發布:2024-05-22 01:30:02 瀏覽:412
什麼軟體可以查電腦配置牌子 發布:2024-05-22 01:19:54 瀏覽:373
廢電腦變成伺服器 發布:2024-05-22 01:19:29 瀏覽:411
c語言函數手冊chm 發布:2024-05-22 01:15:32 瀏覽:871
linuxservice命令 發布:2024-05-22 01:07:56 瀏覽:373
clinux程序設計 發布:2024-05-22 01:04:47 瀏覽:513
armc編程 發布:2024-05-22 01:02:26 瀏覽:158