android獲取控制項
❶ android 關於獲取控制項id的問題 代碼如下:
當然找不到了!因為現在你添加的是setting.xml,所以這個ImageView會默認在setting.xml里找,setting.xml里沒有所以找不到;解決辦法是你必須通過setting.xml把preference_image.xml載入(inflate)進來,如:
LayoutInflater inflater = getLayoutInflater();
View imageLayout = inflater.inflate(R.layout.preference_image.xml,null);然後
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.RightImage);
這只是大致步驟,具體看你的程序
❷ android 怎麼在布局裡面獲取控制項
layout為布局,布局裡面可以放任何空間,獲取空間可以用findViewById方法獲取
android 獲取某個布局控制項 添加到另一個布局中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.layout456);
ImageView imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.ic_launcher);
relativeLayout.addView(imgApple2);
LayoutInflater factorys = LayoutInflater.from(MainActivity.this);
final View textEntryView = factorys.inflate(R.layout.layout1, null);
// LinearLayout linearLayout = (LinearLayout) textEntryView
// .findViewById(R.id.layout1);
// relativeLayout.addView(linearLayout);
EditText editText1 = (EditText) textEntryView
.findViewById(R.id.editText1);
relativeLayout.addView(editText1);
❸ android怎麼獲取 中的控制項
在實際開發中LayoutInflater這個類還是非常有用的,它的作用類似於findViewById()。不同點是LayoutInflater是用來找res/layout/下的xml布局文件代碼塊,並且實例化;而findViewById()是找xml布局文件下的具體widget控制項(如Button、TextView等)。 具體作用:
1、對於一個沒有被載入或者想要動態載入的界面,都需要使用LayoutInflater.inflate()來載入;
2、對於一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。
LayoutInflater 是一個抽象類,在文檔中如下聲明:
public abstract class LayoutInflater extends Object
獲得 LayoutInflater 實例的三種方式:
1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1. LayoutInflater inflater = LayoutInflater.from(context);
其實,這三種方式本質是相同的,從源碼中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其實是調用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater ==null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其實調用 context.getSystemService()。
結論:所以這三種方式最終本質是譽基胡都是調慶攔用的Context.getSystemService()。鋒伍
inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
public View inflate (int resource, ViewGroup root);
3 public View inflate (XmlPullParser parser, ViewGroup root);
4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);
5 public View inflate (int resource, ViewGroup root, boolean attachToRoot);
6
7 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
8 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
9 //EditText editText = (EditText)findViewById(R.id.content);
10 // error
EditText editText = (EditText)view.findViewById(R.id.content);
對於上面代碼,指定了第二個參數 ViewGroup root,當然你也可以設置為 null 值。
注意:
·inflate方法與 findViewById 方法不同;
·inflater 是用來找 res/layout下的 xml 布局文件,並且實例化;
·findViewById() 是找具體 xml 布局文件中的具體 widget 控制項(如:Button、TextView 等)。
❹ 如何獲取android安卓控制項EditText中的內容
Android中Edit text控制項的內容可以添加一個監聽器,來獲鬧喊取內容放到別的控友彎御件上去,示例如下:
package nbe.sense7.vinci.edittext;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class EditTextDemoActivity extends Activity {
private EditText editText;
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.input);
textView = (TextView)findViewById(R.id.output);
/好岩/設置EditText按鍵輸入時的事件
editText.setOnKeyListener(new EditText.OnKeyListener(){
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
textView.setText(editText.getText());//獲取edittext的內容
return false;
}
});
}
}
❺ Android 如何獲取listview中item當中的制定的控制項
listview 要用自定義的適配器 BaseAdapter,然後在適配器的getView方法中對每一個item進行扮銀處理,如果你還不知道什麼叫自定義適配器,建議先查看備鋒listView及適配器的使仿缺晌用方法.
❻ android 怎麼在代碼中獲取控制項的屬性值
如果是自定義的控制項可以用一下代碼TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);// 由attrs 獲得 TypeArray,
如果是系統自帶的控制項,通常控制項點get會有對應的獲取屬性值的方法,如textView.getHeight,不過有些方法如margin,就要通過layoutparam去獲取設置
❼ android單擊button後,獲取某個控制項的值,怎麼獲取
設置一個變數int whichEditText=0;在RadioGroup的監聽事件中設置whichEditText的值
// RadioGroup的監聽事件
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == rb_1.getId()) {
whichEditText= 1;
} else if (checkedId == rb_2.getId()) {
whichEditText= 2;
} else if (checkedId == rb_3.getId()) {
whichEditText = 3;
} else if (checkedId == rb_4.getId()) {
whichEditText= 4;
}else if (checkedId == rb_5.getId()) {
whichEditText= 5;
}
}
然後再在冊爛確神祥認按鈕的監聽事件中,用游姿搏switch語句根據whichEditText的值來選擇獲取哪個EditText的值
❽ android 怎麼獲取控制項的id
控制項的id是自己在xml中定義的, 可以通過R.id.來得到, 如果是view的話,可以直接調用view.getId()即可得到該ID了