這是事情:android - 重疊視圖
我有一個EditText和一個按鈕。這個概念是讓EditText長一點,這樣它就可以覆蓋按鈕,但按鈕是最上面的。讓我們用這種方式描述,[]爲EditText寬度,()爲Button寬度。
[ EditText (Button)]
按鈕位於EditText中的右側。可能嗎? 有什麼建議嗎?謝謝。
這是事情:android - 重疊視圖
我有一個EditText和一個按鈕。這個概念是讓EditText長一點,這樣它就可以覆蓋按鈕,但按鈕是最上面的。讓我們用這種方式描述,[]爲EditText寬度,()爲Button寬度。
[ EditText (Button)]
按鈕位於EditText中的右側。可能嗎? 有什麼建議嗎?謝謝。
您應該創建一個自定義視圖。下面的例子中,edittect有一個權限,當你點擊它時,它清除edittext。
clearable_edit_text.xml
<?xml version=」1.0″ encoding=」utf-8″?>
<RelativeLayout xmlns:android=」http://schemas.android.com/apk/res/android」
android:layout_width=」fill_parent」
android:layout_height=」wrap_content」
>
<EditText
android:id=」@+id/clearable_edit」
android:layout_width=」fill_parent」
android:layout_height=」wrap_content」
android:paddingRight=」35dip」
/>
<Button
android:id=」@+id/clearable_button_clear」
android:layout_width=」30dip」
android:layout_height=」30dip」
android:layout_alignParentRight=」true」
android:background=」@drawable/image_clear」
android:layout_centerVertical=」true」
android:layout_marginRight=」5dip」/>
</RelativeLayout>
Java代碼
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class ClearableEditText extends RelativeLayout
{
LayoutInflater inflater = null;
EditText edit_text;
Button btn_clear;
public ClearableEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initViews();
}
public ClearableEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
initViews();
}
public ClearableEditText(Context context)
{
super(context);
// TODO Auto-generated constructor stub
initViews();
}
void initViews()
{
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.clearable_edit_text, this, true);
edit_text = (EditText) findViewById(R.id.clearable_edit);
btn_clear = (Button) findViewById(R.id.clearable_button_clear);
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
clearText();
showHideClearButton();
}
void clearText()
{
btn_clear.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
edit_text.setText(「」);
}
});
}
void showHideClearButton()
{
edit_text.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
if (s.length() > 0)
btn_clear.setVisibility(RelativeLayout.VISIBLE);
else
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
}
public Editable getText()
{
Editable text = edit_text.getText();
return text;
}
}
所以,現在我們的自定義視圖ClearableEditText準備好現在的地方,我們需要它,我們可以使用它。 like
<?xml version=」1.0″ encoding=」utf-8″?>
<LinearLayout xmlns:android=」http://schemas.android.com/apk/res/android」
android:layout_width=」fill_parent」
android:layout_height=」fill_parent」 >
<com.and.ab1209.ClearableEditText
android:id=」@+id/edit_text_clearable」
android:layout_width=」fill_parent」
android:layout_height=」wrap_content」 />
</LinearLayout>
將這兩個放在相對佈局中,然後將EditText的寬度設置爲fill_parent並將android:layout_alignParentRight設置爲true,實際上就是這樣做的。感謝朋友,爲此+1。 – lorraine
你爲什麼要那樣做?你知道文本可以**按鈕右上方的**?另外,你有什麼嘗試? – shkschneider
顯示xml文件 – NewUser